比较datetime字段时查询速度慢但使用字符串比较时速度快(Mysql)

时间:2014-04-29 20:33:48

标签: mysql sql

我有这个问题:

select DATE_FORMAT(a.Date,'%d/%m/%Y') as data, a.DateTIni, a.DateTEnd, (SELECT count(eepv.id)
                            FROM eventos_extension AS eepv
                            WHERE eepv.idEvento = 1
                            AND eepv.dataHoraEvento between a.DateTIni AND a.DateTEnd) AS visits, 

                           (SELECT count(eee.id)
                            FROM eventos_extension AS eee
                            WHERE eee.idEvento = 3
                            AND eee.dataHoraEvento between a.DateTIni AND a.DateTEnd) AS redirects,

               (SELECT  count(distinct ees.idUnicoEx)
                            FROM eventos_extension AS ees
                            WHERE ees.idEvento = 3
                            AND ees.dataHoraEvento between a.DateTIni AND a.DateTEnd) AS unique_redirects

from (
select  curdate() - INTERVAL (a.a + (10 * b.a) + (100 * c.a)) DAY as Date,
        DATE_ADD(curdate() - INTERVAL (a.a + (10 * b.a) + (100 * c.a)) DAY, INTERVAL '00:00:00' HOUR_SECOND) as DateTIni,
        DATE_ADD(curdate() - INTERVAL (a.a + (10 * b.a) + (100 * c.a)) DAY, INTERVAL '23:59:59' HOUR_SECOND) as DateTEnd

from (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as a
cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as b
cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as c
) a
where a.Date between "2014-04-21" and "2014-04-21"
ORDER BY a.Date

此查询大约需要6秒才能在10mm

的表上执行

但是,如果我将子查询中的比较更改为字符串格式中的相同日期,则只需0.219秒

喜欢这个

select DATE_FORMAT(a.Date,'%d/%m/%Y') as data, a.DateTIni, a.DateTEnd, (SELECT count(eepv.id)
                            FROM eventos_extension AS eepv
                            WHERE eepv.idEvento = 1
                            AND eepv.dataHoraEvento between '2014-04-21 00:00:00' AND '2014-04-21 23:59:59') AS visits, 

                           (SELECT count(eee.id)
                            FROM eventos_extension AS eee
                            WHERE eee.idEvento = 3
                            AND eee.dataHoraEvento between '2014-04-21 00:00:00' AND '2014-04-21 23:59:59') AS redirects,

               (SELECT  count(distinct ees.idUnicoEx)
                            FROM eventos_extension AS ees
                            WHERE ees.idEvento = 3
                            AND ees.dataHoraEvento between '2014-04-21 00:00:00' AND '2014-04-21 23:59:59') AS unique_redirects

from (
select  curdate() - INTERVAL (a.a + (10 * b.a) + (100 * c.a)) DAY as Date,
        DATE_ADD(curdate() - INTERVAL (a.a + (10 * b.a) + (100 * c.a)) DAY, INTERVAL '00:00:00' HOUR_SECOND) as DateTIni,
        DATE_ADD(curdate() - INTERVAL (a.a + (10 * b.a) + (100 * c.a)) DAY, INTERVAL '23:59:59' HOUR_SECOND) as DateTEnd

from (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as a
cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as b
cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as c
) a
where a.Date between "2014-04-21" and "2014-04-21"
ORDER BY a.Date

第一个我做错了什么?

我需要以7天的间隔执行此查询,如果每天需要0.219秒即可,但6秒需要超过40秒才能完成。

0 个答案:

没有答案