我想获得关于什么/如何使用索引和更好的SQL查询的想法和想法。Mysql - 查询和索引
这里是我的SQL查询的示例:
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)
,我试图使用MySQL的EXPLAIN输出:
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
进出口,使用了大量的家伙左加入和连接他们在主键...我的朋友告诉我使用索引来提高速度得到结果..我感到困惑时,我f这些指标减慢了写查询的速度:INSERT,DELETE,UPDATE其中album和albuminfos表可能随时有新的/更新的记录..所以我很迷茫,所以我想倾听并从中获得想法专业:
- 是我查询的好吗?
- 我在mysql EXPLAIN中需要知道些什么?
- 索引是否可以使用?如果是的话。怎么样?
- 正确与错误在我的设置?
谢谢!
所有的表都在使用索引。 PRIMARY表示也是索引的主键。 – DevZer0
你正在使用很多* LEFT JOINs?他们在哪? –
@ypercube我一直以为'从专辑,albumstatus,albumtypes,albuminfos WHERE albums.status = albumstatus.id AND albumtypes.id = albums.albumtype_id AND albums.id = albuminfos.id ' 总是在左连接,正确吗? –