改进这个非常慢的MySQL选择
我有一个非常慢的MySQL查询,我想是因为所有的连接(看起来很复杂,但这是一个很多表的问题): 所有%s都是sprintf()PHP函数中的参数改进这个非常慢的MySQL选择,mysql,indexing,Mysql,Indexing,我有一个非常慢的MySQL查询,我想是因为所有的连接(看起来很复杂,但这是一个很多表的问题): 所有%s都是sprintf()PHP函数中的参数 最重要的是要注意。。。我在MySQL上没有索引!我想我可以加快添加一些索引的过程。。。但是什么,在哪里?有太多的连接和搜索参数,我不知道什么是有效的:-) 请问你能帮忙吗? 提前谢谢 您可以从在where条件中使用的那些列上添加索引开始 此外,您应该为联接中使用的字段编制索引,即主键和外键列 我认为,逐步试验这些指数将产生真正的绩效提升 此外,我还观察
最重要的是要注意。。。我在MySQL上没有索引!我想我可以加快添加一些索引的过程。。。但是什么,在哪里?有太多的连接和搜索参数,我不知道什么是有效的:-) 请问你能帮忙吗?
提前谢谢 您可以从在where条件中使用的那些列上添加索引开始 此外,您应该为联接中使用的字段编制索引,即主键和外键列 我认为,逐步试验这些指数将产生真正的绩效提升 此外,我还观察到,您在单个查询中获取的数据太多。如果确实不需要,请将其拆分为不同的报告和页面(如果可能的话),因为即使您进行了适当的索引,该解决方案也不会具有很强的可扩展性,并且可能无法处理大量数据
注意:您可能必须对通过“%”限定符查询的字段创建全文索引。(即使用LIKE运算符)LIKE运算符的速度非常慢。下面是关于应用索引和全文的讨论
您能提供执行后计划吗<代码>解释查询
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