用户
 找回密码
 入住 CI 中国社区
搜索
查看: 1549|回复: 1
收起左侧

[HELP] 高手帮忙看一下这SQL能怎样优化啊

[复制链接]
发表于 2013-1-8 16:13:37 | 显示全部楼层 |阅读模式
SELECT DISTINCT SQL_CALC_FOUND_ROWS c.child_id, c.child_archive, c.child_name, c.child_birth, c.child_birth_class, c.child_lunar_birth, c.org_id, c.child_level, c.child_status, c.child_sex,
p.parent_id, p.parent_name, p.parent_mobile, p.parent_tel,
IF(DATEDIFF(timestampadd(year, (YEAR(NOW())-YEAR(from_unixtime(c.child_birth))), from_unixtime(c.child_birth)), NOW())>=0, DATEDIFF(timestampadd(year, (YEAR(NOW())-YEAR(from_unixtime(c.child_birth))), from_unixtime(c.child_birth)), NOW()), DATEDIFF(timestampadd(year, (YEAR(NOW())-YEAR(from_unixtime(c.child_birth))+1), from_unixtime(c.child_birth)), NOW())) birth
FROM (child as c)
LEFT JOIN child_folder cf on cf.child_id=c.child_id and cf.user_id=2
LEFT JOIN parent as p ON p.parent_id=c.parent_id
LEFT JOIN child_fin_class as cfc ON cfc.child_id=c.child_id
LEFT JOIN financial_class as fc ON fc.financial_class_id=cfc.financial_class_id and fc.class_type_id=1
where cf.folder_id is null
ORDER BY c.child_id desc
LIMIT 2000;
其中左连接那些ID都已加索引
 楼主| 发表于 2013-1-8 16:24:02 | 显示全部楼层
还有一个问题,就是上面的SQL少了p.parent_name, p.parent_mobile, p.parent_tel 这3个字段会快一倍,不知道为啥

本版积分规则