简体   繁体   English

提高MySQL查询性能(使用explain类型的索引)

[英]Improve Mysql query performance (index in type using explain)

Hi everyone I have the following database: 大家好,我有以下数据库:

`Users`
'id', 'char(36)', 'NO', 'PRI', NULL, ''
'password', 'varchar(64)', 'NO', '', NULL, ''
'email', 'varchar(60)', 'NO', 'UNI', NULL, ''
'roles', 'longtext', 'NO', '', NULL, ''
'is_active', 'tinyint(1)', 'NO', '', NULL, ''

`Galleries`
'id', 'char(36)', 'NO', 'PRI', NULL, ''
'user_id', 'char(36)', 'NO', 'MUL', NULL, ''
'name', 'varchar(255)', 'NO', '', NULL, ''
'description', 'longtext', 'YES', '', NULL, ''
'created_at', 'datetime', 'NO', '', NULL, ''

`Images`
'id', 'char(36)', 'NO', 'PRI', NULL, ''
'gallery_id', 'char(36)', 'YES', 'MUL', NULL, ''
'original_filename', 'varchar(255)', 'NO', '', NULL, ''
'filename', 'varchar(255)', 'NO', '', NULL, ''
'description', 'longtext', 'YES', '', NULL, ''

And the following query: 和以下查询:

SELECT gal.name, gal.description, img.filename, img.description FROM `homestead`.`users` AS users
LEFT JOIN `homestead`.`galleries` AS gal ON users.id = gal.user_id
LEFT JOIN `homestead`.`images` AS img on img.gallery_id = gal.id
WHERE img.description LIKE '%dog%';

When using explain in this query I get the index result in the type field of the Users query. 在此查询中使用explain ,我在用户查询的type字段中获得index结果。 My question is, is there any way to improve this query so it improves this result. 我的问题是,有什么方法可以改善此查询,从而改善此结果。 I understand the index means it goes through all the entries on that particular table, and that may be necessary in this case. 我了解index意味着它会遍历该特定表上的所有条目,在这种情况下可能是必需的。

But do you guys see other ways of improving this? 但是,你们是否看到其他改进方法?

EDIT: Showing indexes for galleries and images tables: 编辑:显示画廊和图像表的索引:

Galleries: 画廊:

'galleries', '0', 'PRIMARY', '1', 'id', 'A', '1', NULL, NULL, '', 'BTREE', '', ''
'galleries', '0', 'UNIQ_F70E6EB7BF396750', '1', 'id', 'A', '1', NULL, NULL, '', 'BTREE', '', ''
'galleries', '1', 'IDX_F70E6EB7A76ED395', '1', 'user_id', 'A', '1', NULL, NULL, '', 'BTREE', '', ''

Images: 图片:

'images', '0', 'PRIMARY', '1', 'id', 'A', '4', NULL, NULL, '', 'BTREE', '', ''
'images', '0', 'UNIQ_E01FBE6ABF396750', '1', 'id', 'A', '4', NULL, NULL, '', 'BTREE', '', ''
'images', '1', 'IDX_E01FBE6A4E7AF8F', '1', 'gallery_id', 'A', '4', NULL, NULL, 'YES', 'BTREE', '', ''

And the explain on the query already without the Users table: 并且没有用户表的查询说明:

'1', 'SIMPLE', 'gal', NULL, 'ALL', 'PRIMARY,UNIQ_F70E6EB7BF396750', NULL, NULL, NULL, '1', '100.00', NULL
'1', 'SIMPLE', 'img', NULL, 'ref', 'IDX_E01FBE6A4E7AF8F', 'IDX_E01FBE6A4E7AF8F', '109', 'homestead.gal.id', '1', '25.00', 'Using where'

First, you can change the left join to inner join -- the where clause is already doing that, but be explicit. 首先,您可以更改left joininner join -在where子句已经这样做,但要明确的。 You can also get rid of users , because it is not used. 您也可以摆脱users ,因为它不被使用。

SELECT g.name, g.description, i.filename, i.description
FROM `homestead`.`galleries` g INNER JOIN 
     `homestead`.`images` i 
     ON i.gallery_id = g.id
WHERE i.description LIKE '%dog%';

This query should be fine with indexes on images(gallery_id) . 此查询应该可以在images(gallery_id)上使用索引。

If performance is really an issue, you may want to investigate full text indexes so you can possibly change the like to match() . 如果性能是一个真正的问题,您可能需要调查全文索引,所以你可以改变可能的like ,以match()

(This partially disagrees with Gordon's answer.) (这与戈登的答案有些不同。)

Since the WHERE only mentions images ( WHERE i.description LIKE '%dog%' ), the Optimizer will probably start with that table. 由于WHERE仅提及imagesWHERE i.description LIKE '%dog%' ),因此优化程序可能从该表开始。 No regular index is useful for that. 没有常规索引对此有用。 (Gordon's comment on FULLTEXT is the 'right' way to improve that.) (戈登对FULLTEXT的评论是改善此问题的“正确”方法。)

The second table is easy to get to if id is the PRIMARY KEY . 如果idPRIMARY KEY则很容易获得第二个表。 Please provide SHOW CREATE TABLE so we can see what indexes you have. 请提供SHOW CREATE TABLE以便我们可以查看您拥有哪些索引。

(I agree with the rest of Gordon's Answer.) (我同意戈登答案的其余部分。)

Another issue... If the tables become 'huge', you will find that UUIDs perform poorly (because of the randomness). 另一个问题...如果表变得“巨大”,您会发现UUID的性能很差(由于随机性)。 A million rows is OK, a billion rows will be very slow. 一百万行可以,十亿行将非常慢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM