简体   繁体   English

改善这个非常慢的MySQL SELECT

[英]Improve this very slow MySQL SELECT

I have this MySQL query that is very slow, I presume because of all the JOINs (it seems complicated, but it's a matter of lot of tables): 我有一个非常慢的MySQL查询,我想是因为所有的JOIN(看起来很复杂,但这是很多表的问题):

SELECT DISTINCT doctors.doc_id, 
    doctors.doc_user, 
    doctors.doc_first, 
    doctors.doc_last, 
    doctors.doc_email, 
    doctors.doc_notes, 
    titles.tit_name, 
    specializations.spe_name, 
    activities.act_name, 
    users.use_first, 
    users.use_last,
    (SELECT COUNT(*) FROM locations WHERE locations.loc_doctor = doctors.doc_id) AS loc_count,
    (SELECT COUNT(*) FROM reception WHERE reception.rec_doctor = doctors.doc_id) AS rec_count,
    (SELECT COUNT(*) FROM visits INNER JOIN reports ON visits.vis_report = reports.rep_id  WHERE visits.vis_doctor = doctors.doc_id AND reports.rep_user LIKE '%s') AS vis_count
FROM
    doctors
INNER JOIN titles ON titles.tit_id = doctors.doc_title
INNER JOIN specializations ON specializations.spe_id = doctors.doc_specialization
INNER JOIN activities ON activities.act_id = doctors.doc_activity
LEFT JOIN locations ON locations.loc_doctor = doctors.doc_id
INNER JOIN users ON doctors.doc_user = users.use_id
WHERE
    ((doctors.doc_last LIKE %s) OR (doctors.doc_first LIKE %s) OR (doctors.doc_email LIKE %s)) 
    AND doctors.doc_user LIKE %s 
    AND locations.loc_province LIKE %s 
    AND doctors.doc_specialization LIKE %s 
    AND doctors.doc_activity LIKE %s 
ORDER BY %s

All the %s are parameters in a sprintf() PHP function 所有%s都是sprintf()PHP函数中的参数
The most important thing to notice is... that I have NO indexes on MySQL! 需要注意的最重要的事情是...我在MySQL上没有索引! I presume that I can speed up the process adding some indexes... but what and where? 我想我可以加快添加一些索引的过程...但是,什么地方? There are so many joins and search parameters that I am in confusion about what would be efficient :-) 有太多的联接和搜索参数,令我困惑的是什么是有效的:-)

Please can you help? 请你帮忙 Thanks in advance! 提前致谢!

You can start with adding indexes on those columns you are using in the where condition. 您可以在您使用在WHERE条件的列添加索引开始。

Further, you should index those fields which are used in join, ie primary keys and foreign keys column. 此外,您应该索引连接中使用的那些字段,即主键和外键列。

I would suggest that gradually experimenting with these indexes would yield a real performance boost. 我建议这些指数逐渐尝试将产生一个真正的性能提升。

Further, I have observed that you are fetching too much of data in a single query. 此外,我发现您在单个查询中获取了太多数据。 If it is really not required, break it up in different reports and pages (If possible) as even if you do proper indexing, the solution will not be quite scalable and may not handle large amount of data. 如果确实不需要,则即使您进行了适当的索引编制,也可以将其分解为不同的报表和页面(如果可能),该解决方案将无法很好地扩展,并且可能无法处理大量数据。

Note: You might have to create full text index on fields which you query by '%' qualifier.(ie use LIKE operator) 注意:您可能必须在通过'%'限定词查询的字段上创建全文索引。(即,使用LIKE运算符)

Like operators are pretty slow. 像运算符一样慢。 Here is a discussion of applying indexes and FULL TEXT 这是关于应用索引和全文的讨论

mysql like performance boost mysql像性能提升

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

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