我的查询意思是:
我不知道1.029 SECs对于这种类型的查询是否正常,或者我做错了什么使得有点慢。如果我做了可以优化的事情,有人可以建议我吗?
因为我的查询需要( 870,968 total, Query took 1.0297 sec)
而实际的查询是
SELECT DATE_FORMAT(FROM_DAYS(TO_DAYS(NOW())-TO_DAYS(STR_TO_DATE(m.m_dob, '%d-%b-%Y'))), '%Y')+0 AS age
FROM impressions AS i, mariners AS m
WHERE STR_TO_DATE(m.m_dob, '%d-%b-%Y') IS NOT NULL
AND m_dob != ''
AND DATE_FORMAT(FROM_DAYS(TO_DAYS(NOW())-TO_DAYS(STR_TO_DATE(m.m_dob, '%d-%b-%Y'))), '%Y')+0 BETWEEN 13 AND 50
ORDER BY `age` DESC
答案 0 :(得分:2)
根据您的评论和当前查询,您实际上并未加入表格。这两个表需要连接在一起:
SELECT DATE_FORMAT(FROM_DAYS(TO_DAYS(NOW())-TO_DAYS(STR_TO_DATE(m.m_dob, '%d-%b-%Y'))), '%Y')+0 AS age
FROM impressions AS i
INNER JOIN mariners AS m
ON i.SessionId = m.ID
WHERE i.impression =1
AND STR_TO_DATE(m.m_dob, '%d-%b-%Y') IS NOT NULL
AND m_dob != ''
AND DATE_FORMAT(FROM_DAYS(TO_DAYS(NOW())-TO_DAYS(STR_TO_DATE(m.m_dob, '%d-%b-%Y'))), '%Y')+0 BETWEEN 13 AND 50
ORDER BY `age` DESC
您当前的查询返回Cartesian result个日期。