Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 包含200.000条以上记录的表格需要4秒时间选择_Mysql - Fatal编程技术网

Mysql 包含200.000条以上记录的表格需要4秒时间选择

Mysql 包含200.000条以上记录的表格需要4秒时间选择,mysql,Mysql,我有一个表,一个用户可以跟随另一个用户。 此表有200.000多条记录。我的选择需要一段时间 where (p.user in (select following from following where user =1 and block=0 and feed=0) or p.user=1) and p.delete='0' 我有一个地方可以找到user=1后面的用户。这是需要更长时间的一部分 我的下表: `following` ( `id` int(11) UNSIGNED NOT N

我有一个表,一个用户可以跟随另一个用户。 此表有200.000多条记录。我的选择需要一段时间

where (p.user in (select following from following where user =1 and block=0 and feed=0) or p.user=1) and p.delete='0'
我有一个地方可以找到user=1后面的用户。这是需要更长时间的一部分

我的下表:

`following` (
  `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
  `user` INT(11) UNSIGNED NOT NULL REFERENCES cadastro (`id`),
  `following` INT(11) UNSIGNED NOT NULL REFERENCES cadastro (`id`),
  `block` tinyint(1) NOT NULL DEFAULT 0,
  `feed` tinyint(1) NOT NULL DEFAULT 0,
  `data` datetime NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY (`user`, `following`)
)
如何更快地获得以下内容并改进此表?对索引或任何东西有什么想法吗

我的完整sql:

select c.nome, p.foto, c.user, p.user, p.id, p.data, p.titulo, p.youtube, pp.foto, count(DISTINCT likes.user) as likes_count, count(distinct comentarios.id) as comentarios_count, count(DISTINCT l2.user) as count2 

from posts p 

join cadastro c on p.user=c.id 
left join profile_picture pp on p.user = pp.user
left join likes on likes.post = p.id
left join comentarios on comentarios.foto = p.id and comentarios.delete = 0  
left join likes l2 on l2.post = p.id and l2.user = ?

where (p.user in (select following from following where user =? and block=0) or p.user=?) and p.delete='0'

group by p.id
order by p.id desc limit ?
职位:

  KEY `share` (post_share, `delete`),
  PRIMARY KEY (`id`)
谢谢


我不确定您是否希望仅在下面的.block=0下停止。在下表中,似乎应该为用户创建一个索引,following,block这将使其成为仅索引扫描

摆脱这个嵌套查询很重要

这里有一个猜测,但我相信您正在寻找以下内容:

select c.nome,
 p.foto,
 c.user,
 p.user,
 p.id,
 p.data,
 p.titulo,
 p.youtube,
 pp.foto,
 count(DISTINCT likes.user) as likes_count,
 count(distinct comentarios.id) as comentarios_count, 
 count(DISTINCT l2.user) as count2 

from posts p 

    join cadastro c on p.user=c.id 
    left join profile_picture pp on p.user = pp.user
    left join likes on likes.post = p.id
    left join comentarios on comentarios.foto = p.id and comentarios.delete = 0  
    left join likes l2 on l2.post = p.id and l2.user = ?

    left join following f on f.user = p.user

where 
(p.user = ? and p.delete = '0')
or 
(f.user = ? and f.block=0)

group by p.id
order by p.id desc limit ?

我们也能得到这个选择的答案吗?@JNevill我刚刚添加了它!posts.user或following.user上是否有索引?从解释上看,这似乎不太可能,我认为这会对这里有很大帮助。可能还喜欢.post,然后可能是comentarios.fotothanks的朋友!我想我没有关于用户的索引。我现在将尝试添加它。这可能是一个内部连接到以下内容,这可能会提高查询性能。