1

I want to get ideas and thoughts about what/how to use indexes and better SQL query.

Here is an example of my SQL query:

SELECT albums.id AS album_id, albums.name AS album_name, albums.upc, albums.status, 
albumstatus.description AS album_status, DATE_FORMAT(albums.created, '%Y-%m-%d') AS created_date,  
albuminfos.label   
FROM albums,albumstatus, albumtypes, albuminfos  
WHERE albums.status = albumstatus.id AND albumtypes.id = albums.albumtype_id AND albums.id  = albuminfos.id  
AND albums.account_id = 9999  
AND albums.status IN (1, 2) 

And I tried using mysql EXPLAIN the output:

id  select_type  table        type    possible_keys                           key         key_len  ref                                              rows  Extra        

 1  SIMPLE       albums       ref     PRIMARY,account_id,status,albumtype_id  account_id  4        const                                            4148  Using where  
 1  SIMPLE       albumstatus  eq_ref  PRIMARY,id                              PRIMARY     4        albums.status             1  Using where  
 1  SIMPLE       albumtypes   eq_ref  PRIMARY                                 PRIMARY     1        albums.albumtype_id       1  Using index  
 1  SIMPLE       albuminfos   eq_ref  PRIMARY                                 PRIMARY     4        albums.id                 1               

Im the guy that uses a lot of LEFT JOIN and connecting them on Primary Key... My friend told me to use indexes to improve increase the speed getting the result.. I got confused when I found that the indexes slow down the speed of writing queries : INSERT,DELETE, UPDATE where album and albuminfos tables may have a new/updated record in anytime.. so I am so lost and so I want to listen and get the ideas from the professional:

  1. is my query good?
  2. What do I need to know in mysql EXPLAIN?
    • Is indexes possible to use? If yes..How?
  3. The right and wrong in my setup?

Thanks!

4

2 回答 2

1

问题一:

将连接与 where 子句分开(它更易于阅读且更合乎逻辑),否则您的查询似乎以旧方式正确。

SELECT albums.id AS album_id, albums.name AS album_name, albums.upc, albums.status, 
    albumstatus.description AS album_status, DATE_FORMAT(albums.created, '%Y-%m-%d') AS created_date,  
    albuminfos.label   
FROM albums
LEFT OUTER JOIN albumstatus sta ON  albums.status = albumstatus.id
LEFT OUTER JOIN albumtypes  typ ON  albumtypes.id = albums.albumtype_id
LEFT OUTER JOIN albuminfos  inf ON  albums.id     = albuminfos.id  
WHERE  albums.account_id = 9999  AND albums.status IN (1, 2) ;

您的索引是正确的。

对于复合索引 (@ypercube):

    ALTER TABLE albums ADD INDEX idx_account_id (account_id ASC, status ASC) ;

问题2 :

强制 MySQL 在 Join 上使用两个索引

但是 MySQL 通常做得很好。

问题 3:

你的专辑里有多少条记录?

于 2013-07-01T07:54:37.527 回答
0

您可以使用“SHOW CREATE TABLE”提供有关表格的更多详细信息吗?

从 EXLPAIN 输出看来,您当前的键(它们是 PRIMARY 键)对于这个 SQL 来说已经足够了。如您所见,EXPLAIN 之外的所有行都表明正在使用一个 KEY(PRIMARY 一个)。

我建议阅读http://dev.mysql.com/doc/refman/5.0/en/explain-output.html以了解如何解释输出,最重要的是要注意,possible_keyNULL(意味着你是不使用任何键进行数据选择),数很少(除非您没有匹配任何内容,否则表中的总行数是可以的),Extra不应该说像使用临时的;使用文件排序,这意味着正在执行较慢的连接/排序/选择/搜索。

于 2013-07-01T07:51:15.417 回答