我遇到了SQL查詢的問題。當值更改時選擇sql行
我正在使用MariaDB。我有一個包含多個字段的表,我希望在更改值時選擇行。
例如:
+------------+----------+----------+
| time_stamp | G1.state | G2.state |
+------------+----------+----------+
| 1433717821 | 3 | 1 |
+------------+----------+----------+
| 1433717881 | 3 | 1 |
+------------+----------+----------+
| 1433717942 | 5 | 1 |
+------------+----------+----------+
在這裏,我需要得到3個時變爲5.我已經試過以下查詢TIME-STAMP:
SELECT a.`time_stamp`, a.`G1.state`, `G2.state`
FROM `regs` AS `a`
WHERE ((a.`G1.Estado` <>
(SELECT b.`G1.state`
FROM `regs` AS `b`
WHERE a.time_stamp > b.time_stamp
ORDER BY b.time_stamp DESC
LIMIT 1)) OR
(a.`G2.state` <>
(SELECT b.`G2.state`
FROM `regs` AS `b`
WHERE a.time_stamp > b.time_stamp
ORDER BY b.time_stamp DESC
LIMIT 1))) AND
(a.time_stamp >= 1433159549) AND (a.time_stamp < 1433677949)
ORDER BY a.time_stamp ASC
但是它只是需要太長時間運行,它需要50.96秒,我需要更快(希望不到一秒)。它分析了10k左右的值。任何人都可以在這裏給我一隻手嗎?
您使用哪個dbms? (例如,LIMIT是特定產品...) – jarlh
那麼對於那3行,你只想要最後一個返回? – jarlh
請添加您的桌子上有哪些索引。 – whyte624