我在 MySql 中遇到了对大量数据进行查询处理的问题。我需要通过连接函数将数据提取到四个以上的表中。查询在服务器上运行速度非常慢。
如何优化多个连接查询的处理时间。
我目前正在使用 innodb 引擎。批量数据表是可以的。
我有这样的表格
这些表中的数据 exam,students,subjects,subject_tests,subject_test_mark,total_subject,
我得到所有学生当前考试的记录。
这之前会通过多个 for loops 处理。连续的数据库访问。这是放慢我的进程的一个原因。如何通过 SQL 避免这些情况。向我提出一些受欢迎的想法。
我对此有些怀疑。表引擎innodb好不好。索引是否支持这个过程?
SELECT `sub`.student_id,
((sum( `sub`.total / `main`.max_total )) / count( sub.id )) *10 AS percent,
GROUP_CONCAT((sub.total / main.max_total) *10 ORDER BY (sub.total / main.max_total) *10 DESC SEPARATOR ',' ) AS marks
FROM
`cmark_batch` AS `main`
LEFT JOIN `cmark_para_total` AS `sub`
ON `sub`.mark_batch_id = `main`.id
WHERE main.batch_id =29
AND main.term =4
AND main.subject_id =64
AND main.status = 'A'
GROUP BY student_id
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE main ref search search 8 const 85 Using index condition; Using where; Using temporary; Using filesort
1 SIMPLE sub ref finder finder 8 newlive.main.id 14 NULL
SELECT t1.mark_batch_id, t1.id, t1.param_name, t1.max_mark,t2.student, t2.mark
FROM `cmark_parameter` AS t1
LEFT JOIN `cmark_parameter_mark` AS t2 ON t1.id = t2.mark_parameter_id
WHERE t1.mark_batch_id
IN (621,620,623,622)
AND t1.status = 'A'
AND t2.status = 'A'
ORDER BY `t2`.`student` ASC
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t2 ALL NULL NULL NULL NULL 78835 Using where; Using filesort
1 SIMPLE t1 eq_ref PRIMARY PRIMARY 8 newlive.t2.cmark_parameter_id 1 Using where
SELECT t1.student_id, t1.mark_batch_id, t1.total
FROM `cmark_para_total` AS t1
WHERE t1.mark_batch_id
IN (621,620,623,622)
AND t1.status = 'A'
ORDER BY `t1`.`id` ASC
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 range finder finder 8 NULL 111 Using index condition; Using where; Using filesort
最佳答案
您可以使用数据库缓存来减少加载复杂查询的时间。 在 Mysql 手册中描述了如何启用数据库缓存:
http://dev.mysql.com/doc/refman/5.1/en/query-cache.html
在您的查询中,您必须添加属性 SQL_CACHE 以启用缓存,例如:
SELECT SQL_CACHE id, name FROM customer;
关于php - 喜欢一些MySql Optimization techniques for Bulk data table,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23008547/