How to improve performance with an ORDER BY clause












0














I have a query that is reading through approximately 2.4m rows of data.



The query itself is running well but the ORDER BY clause is causing performance issues. If I remove the ORDER BY the query takes 0.03 seconds to execute. With the ORDER BY it can take 4.5 to 5 seconds.



Is there anyway I an optimise this query further? Indexes have been added so that isn't a solution.



EDIT 1 -
This query is a shortened version of a much bigger PDO query so I think the join is necessary. You can see the main query at the bottom of this post.



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']);












share|improve this question
























  • which version of mysql are you using?
    – Radim Bača
    Nov 12 at 12:25










  • Version 8.0 mate
    – Dally
    Nov 12 at 14:02
















0














I have a query that is reading through approximately 2.4m rows of data.



The query itself is running well but the ORDER BY clause is causing performance issues. If I remove the ORDER BY the query takes 0.03 seconds to execute. With the ORDER BY it can take 4.5 to 5 seconds.



Is there anyway I an optimise this query further? Indexes have been added so that isn't a solution.



EDIT 1 -
This query is a shortened version of a much bigger PDO query so I think the join is necessary. You can see the main query at the bottom of this post.



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']);












share|improve this question
























  • which version of mysql are you using?
    – Radim Bača
    Nov 12 at 12:25










  • Version 8.0 mate
    – Dally
    Nov 12 at 14:02














0












0








0







I have a query that is reading through approximately 2.4m rows of data.



The query itself is running well but the ORDER BY clause is causing performance issues. If I remove the ORDER BY the query takes 0.03 seconds to execute. With the ORDER BY it can take 4.5 to 5 seconds.



Is there anyway I an optimise this query further? Indexes have been added so that isn't a solution.



EDIT 1 -
This query is a shortened version of a much bigger PDO query so I think the join is necessary. You can see the main query at the bottom of this post.



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']);












share|improve this question















I have a query that is reading through approximately 2.4m rows of data.



The query itself is running well but the ORDER BY clause is causing performance issues. If I remove the ORDER BY the query takes 0.03 seconds to execute. With the ORDER BY it can take 4.5 to 5 seconds.



Is there anyway I an optimise this query further? Indexes have been added so that isn't a solution.



EDIT 1 -
This query is a shortened version of a much bigger PDO query so I think the join is necessary. You can see the main query at the bottom of this post.



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']);








$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']);





$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']);






mysql database sql-order-by






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 14:06

























asked Nov 12 at 12:25









Dally

135118




135118












  • which version of mysql are you using?
    – Radim Bača
    Nov 12 at 12:25










  • Version 8.0 mate
    – Dally
    Nov 12 at 14:02


















  • which version of mysql are you using?
    – Radim Bača
    Nov 12 at 12:25










  • Version 8.0 mate
    – Dally
    Nov 12 at 14:02
















which version of mysql are you using?
– Radim Bača
Nov 12 at 12:25




which version of mysql are you using?
– Radim Bača
Nov 12 at 12:25












Version 8.0 mate
– Dally
Nov 12 at 14:02




Version 8.0 mate
– Dally
Nov 12 at 14:02












2 Answers
2






active

oldest

votes


















0














Perhaps I'm missing something, but I don't see that the subquery requires the joins. Does this suffice?



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


If so, an index on transactionsV2(processing_time) would help (assuming that it is not a view).






share|improve this answer





















  • Sorry I should've mentioned this earlier. This query is actually a shortened version of a much bigger query that contains more where clauses so it is required.
    – Dally
    Nov 12 at 14:04



















0














I believe that the subquery is redundant since it is independent subquery and you are doing the join according to a primary key (transactionsV2.id). You can simply use



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





share|improve this answer





















  • Sorry I should've mentioned this earlier. This query is actually a shortened version of a much bigger query that contains more where clauses so it is required.
    – Dally
    Nov 12 at 14:04











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53262164%2fhow-to-improve-performance-with-an-order-by-clause%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Perhaps I'm missing something, but I don't see that the subquery requires the joins. Does this suffice?



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


If so, an index on transactionsV2(processing_time) would help (assuming that it is not a view).






share|improve this answer





















  • Sorry I should've mentioned this earlier. This query is actually a shortened version of a much bigger query that contains more where clauses so it is required.
    – Dally
    Nov 12 at 14:04
















0














Perhaps I'm missing something, but I don't see that the subquery requires the joins. Does this suffice?



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


If so, an index on transactionsV2(processing_time) would help (assuming that it is not a view).






share|improve this answer





















  • Sorry I should've mentioned this earlier. This query is actually a shortened version of a much bigger query that contains more where clauses so it is required.
    – Dally
    Nov 12 at 14:04














0












0








0






Perhaps I'm missing something, but I don't see that the subquery requires the joins. Does this suffice?



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


If so, an index on transactionsV2(processing_time) would help (assuming that it is not a view).






share|improve this answer












Perhaps I'm missing something, but I don't see that the subquery requires the joins. Does this suffice?



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


If so, an index on transactionsV2(processing_time) would help (assuming that it is not a view).







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 12 at 12:27









Gordon Linoff

755k35290398




755k35290398












  • Sorry I should've mentioned this earlier. This query is actually a shortened version of a much bigger query that contains more where clauses so it is required.
    – Dally
    Nov 12 at 14:04


















  • Sorry I should've mentioned this earlier. This query is actually a shortened version of a much bigger query that contains more where clauses so it is required.
    – Dally
    Nov 12 at 14:04
















Sorry I should've mentioned this earlier. This query is actually a shortened version of a much bigger query that contains more where clauses so it is required.
– Dally
Nov 12 at 14:04




Sorry I should've mentioned this earlier. This query is actually a shortened version of a much bigger query that contains more where clauses so it is required.
– Dally
Nov 12 at 14:04













0














I believe that the subquery is redundant since it is independent subquery and you are doing the join according to a primary key (transactionsV2.id). You can simply use



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





share|improve this answer





















  • Sorry I should've mentioned this earlier. This query is actually a shortened version of a much bigger query that contains more where clauses so it is required.
    – Dally
    Nov 12 at 14:04
















0














I believe that the subquery is redundant since it is independent subquery and you are doing the join according to a primary key (transactionsV2.id). You can simply use



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





share|improve this answer





















  • Sorry I should've mentioned this earlier. This query is actually a shortened version of a much bigger query that contains more where clauses so it is required.
    – Dally
    Nov 12 at 14:04














0












0








0






I believe that the subquery is redundant since it is independent subquery and you are doing the join according to a primary key (transactionsV2.id). You can simply use



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





share|improve this answer












I believe that the subquery is redundant since it is independent subquery and you are doing the join according to a primary key (transactionsV2.id). You can simply use



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






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 12 at 12:30









Radim Bača

8,30111125




8,30111125












  • Sorry I should've mentioned this earlier. This query is actually a shortened version of a much bigger query that contains more where clauses so it is required.
    – Dally
    Nov 12 at 14:04


















  • Sorry I should've mentioned this earlier. This query is actually a shortened version of a much bigger query that contains more where clauses so it is required.
    – Dally
    Nov 12 at 14:04
















Sorry I should've mentioned this earlier. This query is actually a shortened version of a much bigger query that contains more where clauses so it is required.
– Dally
Nov 12 at 14:04




Sorry I should've mentioned this earlier. This query is actually a shortened version of a much bigger query that contains more where clauses so it is required.
– Dally
Nov 12 at 14:04


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53262164%2fhow-to-improve-performance-with-an-order-by-clause%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

List item for chat from Array inside array React Native

Thiostrepton

Caerphilly