我有一个SQL查询,用于显示带分页和排序顺序的数据列表。排序顺序有一个" IN"在树中查询以显示更准确的数据。
以下是代码:
SELECT
s.ID id_sent,
SendingDateTime,
DestinationNumber,
TextDecoded,
UDH,
id_folder,
Status,
p.ID id_pbk,
Name,
Number
FROM
sentitems s LEFT JOIN pbk p
ON p.Number = s.DestinationNumber
WHERE
`id_folder` = 3
AND `Status` IN ('SendingOK', 'SendingOKNoReport')
AND SendingDateTime IN (
SELECT
MAX(SendingDateTime)
FROM sentitems
WHERE
id_folder = '3'
AND `Status` IN ('SendingOK', 'SendingOKNoReport')
GROUP BY DestinationNumber
)
ORDER BY `SendingDateTime` DESC LIMIT $posisi,$batas
我的问题是,当我在sentitems表中有很多数据时; ex 3000数据,虽然我限制20个数据显示。该页面确实需要很长时间才能加载然后超时。
我的问题是,是否可以改进我的查询? 或者是否有任何其他方式来显示我的数据结果与我的查询完全相同?
提前致谢
答案 0 :(得分:0)
这可能适合你
SELECT s.ID id_sent, SendingDateTime, DestinationNumber, TextDecoded, UDH, id_folder, Status, p.ID id_pbk, Name, Number
FROM sentitems s
LEFT JOIN pbk p ON p.Number = s.DestinationNumber
inner join
(
SELECT MAX(SendingDateTime) as maxSendingDateTime
FROM sentitems
WHERE id_folder = '3'
AND `Status`
IN (
'SendingOK', 'SendingOKNoReport'
)
GROUP BY DestinationNumber
) c as c.maxSendingDateTime = maxSendingDateTime
WHERE `id_folder` = 3
AND `Status`
IN (
'SendingOK', 'SendingOKNoReport'
)
ORDER BY `SendingDateTime` DESC
LIMIT $posisi,$batas;
答案 1 :(得分:0)
我的猜测是减速是IN语句,你正在查看MAX(SendingDateTime)列表。相反,您可以尝试使用另一个WHERE子句解析该列表。所以它会是这样的:
SELECT
s.ID id_sent,
SendingDateTime,
DestinationNumber,
TextDecoded,
UDH,
id_folder,
Status,
p.ID id_pbk,
Name,
Number
FROM
sentitems s LEFT JOIN pbk p
ON p.Number = s.DestinationNumber
WHERE
`id_folder` = 3
AND `Status` IN ('SendingOK', 'SendingOKNoReport')
AND SendingDateTime = (
SELECT
MAX(SendingDateTime)
FROM sentitems si
WHERE
si.id_folder = '3'
AND s.Destination = si.Destination
AND si.`Status` IN ('SendingOK', 'SendingOKNoReport')
GROUP BY DestinationNumber
)
ORDER BY `SendingDateTime` DESC LIMIT $posisi,$batas