[英]mysql:best way to optimize this sql query
i want to get best way to get result from this query 我想以最好的方式从此查询中获取结果
here is my tables instructrue 这是我的桌子指示
schools 学校
CREATE TABLE `schools` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user` mediumint(5) NOT NULL,
`gender` tinyint(1) NOT NULL,
`time` int(11) NOT NULL,
`status` tinyint(2) NOT NULL,
`number` mediumint(6) NOT NULL,
`name` varchar(75) NOT NULL,
`address` varchar(75) NOT NULL,
`admin` varchar(50) NOT NULL,
`admin_phone` varchar(20) NOT NULL,
`admin_email` varchar(30) NOT NULL,
`school_phone` varchar(20) NOT NULL,
`learn` tinyint(2) NOT NULL,
`mr7la` tinyint(2) NOT NULL,
`sfof` smallint(3) NOT NULL,
`fswl` smallint(3) NOT NULL,
`json` text NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `user` (`user`),
KEY `status` (`status`),
KEY `learn` (`learn`),
KEY `mr7la` (`mr7la`),
KEY `number` (`number`)
) ENGINE=MyISAM AUTO_INCREMENT=20 DEFAULT CHARSET=utf8
3agz 3agz
CREATE TABLE `3agz` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`school` int(11) NOT NULL,
`tkss` int(11) NOT NULL,
`teacher_7ess` int(11) NOT NULL,
`teacher_master_7ess` int(11) NOT NULL,
`time_added` int(11) NOT NULL,
`reported` int(11) NOT NULL DEFAULT '0',
`fixed` int(11) NOT NULL,
`info` text NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `school` (`school`,`tkss`,`teacher_7ess`,`fixed`),
KEY `school_2` (`school`),
KEY `tkss` (`tkss`),
KEY `reported` (`reported`),
KEY `time_added` (`time_added`),
KEY `school_3` (`school`,`time_added`),
KEY `school_4` (`school`,`fixed`)
) ENGINE=InnoDB AUTO_INCREMENT=85 DEFAULT CHARSET=utf8
here's SQL Fiddle for this 这是为此的SQL小提琴
http://sqlfiddle.com/#!2/3313e0/4 http://sqlfiddle.com/#!2/3313e0/4
you can see her's my sql query i use 你可以看到她是我使用的SQL查询
SELECT
schools.* , ( select count(id) from 3agz where 3agz.school = schools.id and fixed = 0 ) as has_3agz
FROM
schools
WHERE
( select count(id) from 3agz where 3agz.school = schools.id and fixed = 0 ) > 0
limit 10
the explain is 的解释是
schools => PRIMARY : ALL 学校=>小学:全部
3agz => DEPENDENT SUBQUERY : ref 3agz => DEPENDENT子查询:ref
3agz => DEPENDENT SUBQUERY : ref 3agz => DEPENDENT子查询:ref
here's i ask can i do this and what's the best way 这是我问我可以这样做吗,什么是最好的方法
1 - can i get ignore second sub query on where and get it depended on first sub query in select 1-我可以忽略第二个子查询的位置并取决于选择中的第一个子查询吗
2- if number 1 answer is you can't can i ignore the first sub query [ has_3agz alias ] after this query executed i loop trow the result [schools ids] 2-如果数字1的答案是您不能执行此查询后,我不能忽略第一个子查询[has_3agz别名],我循环抛出结果[schools ids]
and make second query like this 然后像这样进行第二次查询
as example the first query return school ids 1 , 2 , 3 , 4 例如,第一个查询返回的学校ID为1、2、3、4
select school , count(id) from 3agz where school in ( 1 , 2 , 3 , 4 ) and fixed = 0
the attach every count to it's school in array 将每个计数附加到数组中的学校
hope you understand me 希望你能理解我
Best regards 最好的祝福
SELECT schools.*, count(*) as has_3agz from schools
LEFT JOIN 3agz on 3agz.school = schools.id and fixed = 0
GROUP BY schools.id;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.