我有一个正在读取大约240万行数据的查询。
查询本身运行良好,但order by子句导致性能问题。如果通过查询删除订单,则执行该查询需要0.03秒。按顺序,可能需要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条答案
按热度按时间ar7v8xwq1#
我认为子查询是多余的,因为它是独立的子查询,并且您是根据主键进行连接的(
transactionsV2.id
). 你可以简单地使用nkoocmlb2#
也许我遗漏了一些东西,但我看不出子查询需要
join
s。这够了吗?如果是,则在
transactionsV2(processing_time)
会有帮助(假设它不是视图)。