樣品查詢加快我的子查詢:如何優化或MySQL或在PHP
SELECT
table1.t1_id,table1.name,
table2.address,
(
SELECT message
FROM table3
WHERE logid = table1.t1_id
AND message NOT LIKE "[ SYSTEM%"
ORDER BY logs
DESC LIMIT 1
) as message
FROM table1
INNER JOIN table2
ON table1.t1_id = table2.t2_id
WHERE table1.dateCreated
BETWEEN CAST('2015-01-01' as Date)
AND CAST('2015-05-30' as Date)
ORDER BY table1.dateCreated DESC
預期輸出:
id | name | address | message |
注:假設表1和表2有幾千行和表3有數百萬行
您的查詢執行多少時間?我的意思是基準。 –
顯示基於phpmyadmin的行0 - 29(總共1,002次,查詢耗時27.2299秒)。 –
你能以創建表的形式發佈一個SQL小提琴或你的數據庫模式,並插入帶有示例數據的語句嗎? –