如何使用ORDER BY子句提高性能

时间:2018-11-12 12:25:04

标签: mysql database sql-order-by

我有一个查询正在读取大约240万行数据。

查询本身运行良好,但是ORDER BY子句导致性能问题。如果删除ORDER BY,查询将花费0.03秒的时间来执行。使用ORDER BY可能需要4.5到5秒钟。

我是否仍在进一步优化此查询?已添加索引,所以这不是解决方案。

编辑1- 该查询是更大的PDO查询的简化版本,因此我认为联接是必要的。您可以在这篇文章的底部看到主要查询。

SELECT t.processing_time, t.paymentType, t.status, t.merchantTransactionId, t.paymentBrand, t.amount, t.currency, t.code, t.holder, t.bin, t.last4Digits, t.recurringType, m.name AS merchant, c.name AS channel, concat(UPPER(SUBSTRING(trim(sp.status_description),1,1)), lower(SUBSTRING(trim(sp.status_description),2))) as status_description
FROM transactionsV2 t
JOIN channels c
ON t.entityId = c.uuid
JOIN merchants m
ON m.uuid = c.sender
JOIN status_payments sp 
ON t.code = sp.status_code
JOIN (
    SELECT t.id, t.processing_time FROM transactionsV2 t
    JOIN channels c ON t.entityId = c.uuid
    JOIN merchants m ON m.uuid = c.sender
    WHERE (t.processing_time >= "2018-11-08 00:00:00")
    AND (t.processing_time <= "2018-11-12 23:59:59")
    ORDER BY t.processing_time DESC
    LIMIT 1000
) t2
ON t.id = t2.id
WHERE t.status = 1

$transactions = DB::connection('mysql2')->select(DB::raw("SELECT t.processing_time, t.paymentType, t.status, t.merchantTransactionId, t.paymentBrand, t.amount, t.currency, t.code, t.holder, t.bin, t.last4Digits, t.recurringType, m.name AS merchant, c.name AS channel, concat(UPPER(SUBSTRING(trim(sp.status_description),1,1)), lower(SUBSTRING(trim(sp.status_description),2))) as status_description
FROM transactionsV2 t
JOIN channels c
ON t.entityId = c.uuid
JOIN merchants m
ON m.uuid = c.sender
JOIN status_payments sp 
ON t.code = sp.status_code
JOIN (
    SELECT t.id, t.processing_time FROM transactionsV2 t
    JOIN channels c ON t.entityId = c.uuid
    JOIN merchants m ON m.uuid = c.sender
    WHERE (t.processing_time >= :insTs1)
    AND (t.processing_time <= :insTs2)
    AND (:merchant1 IS NULL OR m.name LIKE :merchant2)
    AND (:channel1 IS NULL OR c.name LIKE :channel2)
    ORDER BY t.processing_time DESC
    LIMIT 1000
) t2
ON t.id = t2.id
WHERE (:status1 IS NULL OR t.status = :status2)
AND (:holder1 IS NULL OR holder LIKE :holder2)
AND (:paymentType1 IS NULL OR t.paymentType IN (".$paymentType."))
AND (:merchantTransactionId1 IS NULL OR merchantTransactionId LIKE :merchantTransactionId2)
AND (:paymentBrand1 IS NULL OR paymentBrand LIKE :paymentBrand2)
AND (:amount1 IS NULL OR amount = :amount2)
AND (:recurringType1 IS NULL OR t.recurringType = :recurringType2)"),
['status1' => $search->searchCriteria['status'],
'status2' => $search->searchCriteria['status'],
'holder1' => $search->searchCriteria['holder'],
'holder2' => '%'.$search->searchCriteria['holder'].'%',
'paymentType1' => $paymentType,
'merchantTransactionId1' => $search->searchCriteria['merchantTransactionId'],
'merchantTransactionId2' => '%'.$search->searchCriteria['merchantTransactionId'].'%',
'paymentBrand1' => $search->searchCriteria['paymentBrand'],
'paymentBrand2' => '%'.$search->searchCriteria['paymentBrand'].'%',
'amount1' => $search->searchCriteria['amount'],
'amount2' => $search->searchCriteria['amount'],
'recurringType1' => $search->searchCriteria['recurringType'],
'recurringType2' => $search->searchCriteria['recurringType'],
'merchant1' => $search->searchCriteria['merchant'],
'merchant2' => '%'.$search->searchCriteria['merchant'].'%',
'channel1' => $search->searchCriteria['channel'],
'channel2' => '%'.$search->searchCriteria['channel'].'%',
'insTs1' => $search->searchCriteria['fromDate'] . ' 00:00:00',
'insTs2' => $search->searchCriteria['toDate'] . ' 23:59:59']);

2 个答案:

答案 0 :(得分:0)

也许我遗漏了一些东西,但是我没有看到子查询需要join。这样就够了吗?

SELECT t.id, t.processing_time
FROM transactionsV2 t
WHERE t.processing_time >= '2018-11-08' AND
      t.processing_time <= '2018-11-13'
ORDER BY t.processing_time DESC
LIMIT 1000

如果是这样,则transactionsV2(processing_time)上的索引会有所帮助(假设它不是视图)。

答案 1 :(得分:0)

我认为子查询是多余的,因为它是独立的子查询,并且您正在根据主键(transactionsV2.id)进行联接。您可以简单地使用

SELECT t.processing_time, 
       t.paymentType, 
       t.status, 
       t.merchantTransactionId, 
       t.paymentBrand, 
       t.amount, 
       t.currency, 
       t.code, 
       t.holder, 
       t.bin, 
       t.last4Digits, 
       t.recurringType, 
       m.name AS merchant, 
       c.name AS channel, 
       concat(UPPER(SUBSTRING(trim(sp.status_description),1,1)), 
       lower(SUBSTRING(trim(sp.status_description),2))) as status_description,
       row_number() over ()
FROM transactionsV2 t
JOIN channels c ON t.entityId = c.uuid
JOIN merchants m ON m.uuid = c.sender
WHERE (t.processing_time >= "2018-11-08 00:00:00") AND (t.processing_time <= "2018-11-12 23:59:59") and t.status = 1
ORDER BY t.processing_time DESC
LIMIT 1000  
相关问题