sql - 复杂的查询使网站速度极慢

标签 sql mysql

 select SQL_CALC_FOUND_ROWS DISTINCT media.*, username 
from album as album, album_permission as permission, user as user, media as media , word_tag as word_tag, tag as tag 
where ((media.album_id = album.album_id and album.private = 'yes' and album.album_id = permission.album_id and (permission.email = '' or permission.user_id = '') ) or (media.album_id = album.album_id and album.private = 'no' ) or media.album_id = '0' ) 
and media.status = '1' 
and media.user_id = user.user_id 
and word_tag.media_id = media.media_id 
and word_tag.tag_id = tag.tag_id 
and tag.name in ('justin','bieber','malfunction','katherine','heigl','wardrobe','cinetube') and media.media_type = 'video' 
and media.media_id not in ('YHL6a5z8MV4') 
group by media.media_id 
order by RAND()
#there is limit too, by 20 rows..

我不知道从哪里开始解释这个问题,但是请原谅我,如果您有任何问题,请询问我。以下是解释。

SQL_CALC_FOUND_ROWS 正在计算有多少行并将用于分页,因此它会计算总记录,即使只显示 20 条。

DISTINCT 将停止显示重复的行。

用户名来自用户表。

相册,album_permission。它检查相册是否是私有(private)的,如果是,则通过 user_id 检查用户是否有权限。

我认为休息很容易理解,但如果您需要了解更多,请询问。

我对此查询感到非常沮丧,并且由于此查询,网站有时非常慢或无法打开。请帮忙

SQL query: EXPLAIN select SQL_CALC_FOUND_ROWS DISTINCT media.*, username from album as album, album_permission as permission, user as user, media as media , word_tag as word_tag, tag as tag where ((media.album_id = album.album_id and album.private = 'yes' and album.album_id = permission.album_id and (permission.email = '' or permission.user_id = '') ) or (media.album_id = album.album_id and album.private = 'no' ) or media.album_id = '0' ) and media.status = '1' and media.user_id = user.user_id and word_tag.media_id = media.media_id and word_tag.tag_id = tag.tag_id and tag.name in ('justin','bieber','malfunction','katherine','heigl','wardrobe','cinetube') and media.media_type = 'video' and media.media_id not in ('YHL6a5z8MV4') group by media.media_id order by RAND() ;
Rows: 6
id  select_type  table  type  possible_keys  key  key_len  ref  rows  Extra
1  SIMPLE  permission  system  album_id  NULL  NULL  NULL  1  Using temporary; Using filesort
1  SIMPLE  album  ALL  PRIMARY  NULL  NULL  NULL  68   
1  SIMPLE  word_tag  ALL  media_id  NULL  NULL  NULL  88383  Using where; Using join buffer
1  SIMPLE  media  eq_ref  media_id,album_id  media_id  34  _site.word_tag.media_id  1  Using where
1  SIMPLE  tag  eq_ref  PRIMARY  PRIMARY  4  _site.word_tag.tag_id  1  Using where
1  SIMPLE  user  eq_ref  PRIMARY  PRIMARY  34  _site.media.user_id  1   

表结构http://pastie.org/912388这个链接有表转储,您可以在 phpmyadmin 中执行或运行它并查看表的结构。

最佳答案

您能做的最好的事情就是运行查询,但在其前面放置一个EXPLAIN,然后分析结果。

如果不知道该查询中所有表的结构(尤其是索引),我们将很难给您一个好的答案。

从这里开始:Optimizing Queries with EXPLAIN .

如果您按照此处的建议无法取得任何进展,请发布您的表格结构,我们也许可以为您提供帮助。

关于sql - 复杂的查询使网站速度极慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2611717/

相关文章:

sql - 获取oracle中的记录数

php - 与 echo 一起使用时在表数据上放置一个超链接

PHP mysql 而不是根据数据库键分配变量

php - 有没有更智能的方法来使用 PHP 和 MySQL 加载多对多关系?

php - list() 函数和 mysqli_fetch_array 一起工作吗?

MYSQL - 分组并选择最高结果并使用 JOIN?

c# - 创建新的数据库并填充另一个数据库

mysql - 复合键索引

php - 如何以矩阵模式访问 MySQL 表中的值?

php - PGM Mysql PDO WHEN 或 IF 语句