Reduce the execution time in postgresql












1















Here I attached my query. It's getting 60141 ms to execute. I don't know what should i do. But I want to execute in short time.



SELECT id 
FROM activitylog
WHERE (url = '/staff/save/117' OR url = '/staff/create/117')
AND timestamp > '1990-01-01 00:00:00'
AND userid IN ( SELECT id
FROM users
WHERE companyid = ( SELECT companyid
FROM users
WHERE id='150' ) )
ORDER BY timestamp DESC


Thanks In advance.










share|improve this question




















  • 6





    Please edit your question and add the create table statements for the tables in question (including all indexes) and the execution plan generated using explain (analyze, buffers) (not just a "simple" explain). Formatted text please, no screen shots or upload the plan to explain.depesz.com

    – a_horse_with_no_name
    Nov 22 '18 at 12:11
















1















Here I attached my query. It's getting 60141 ms to execute. I don't know what should i do. But I want to execute in short time.



SELECT id 
FROM activitylog
WHERE (url = '/staff/save/117' OR url = '/staff/create/117')
AND timestamp > '1990-01-01 00:00:00'
AND userid IN ( SELECT id
FROM users
WHERE companyid = ( SELECT companyid
FROM users
WHERE id='150' ) )
ORDER BY timestamp DESC


Thanks In advance.










share|improve this question




















  • 6





    Please edit your question and add the create table statements for the tables in question (including all indexes) and the execution plan generated using explain (analyze, buffers) (not just a "simple" explain). Formatted text please, no screen shots or upload the plan to explain.depesz.com

    – a_horse_with_no_name
    Nov 22 '18 at 12:11














1












1








1


1






Here I attached my query. It's getting 60141 ms to execute. I don't know what should i do. But I want to execute in short time.



SELECT id 
FROM activitylog
WHERE (url = '/staff/save/117' OR url = '/staff/create/117')
AND timestamp > '1990-01-01 00:00:00'
AND userid IN ( SELECT id
FROM users
WHERE companyid = ( SELECT companyid
FROM users
WHERE id='150' ) )
ORDER BY timestamp DESC


Thanks In advance.










share|improve this question
















Here I attached my query. It's getting 60141 ms to execute. I don't know what should i do. But I want to execute in short time.



SELECT id 
FROM activitylog
WHERE (url = '/staff/save/117' OR url = '/staff/create/117')
AND timestamp > '1990-01-01 00:00:00'
AND userid IN ( SELECT id
FROM users
WHERE companyid = ( SELECT companyid
FROM users
WHERE id='150' ) )
ORDER BY timestamp DESC


Thanks In advance.







postgresql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 12:11









a_horse_with_no_name

295k46451546




295k46451546










asked Nov 22 '18 at 12:02









Vi JVi J

427




427








  • 6





    Please edit your question and add the create table statements for the tables in question (including all indexes) and the execution plan generated using explain (analyze, buffers) (not just a "simple" explain). Formatted text please, no screen shots or upload the plan to explain.depesz.com

    – a_horse_with_no_name
    Nov 22 '18 at 12:11














  • 6





    Please edit your question and add the create table statements for the tables in question (including all indexes) and the execution plan generated using explain (analyze, buffers) (not just a "simple" explain). Formatted text please, no screen shots or upload the plan to explain.depesz.com

    – a_horse_with_no_name
    Nov 22 '18 at 12:11








6




6





Please edit your question and add the create table statements for the tables in question (including all indexes) and the execution plan generated using explain (analyze, buffers) (not just a "simple" explain). Formatted text please, no screen shots or upload the plan to explain.depesz.com

– a_horse_with_no_name
Nov 22 '18 at 12:11





Please edit your question and add the create table statements for the tables in question (including all indexes) and the execution plan generated using explain (analyze, buffers) (not just a "simple" explain). Formatted text please, no screen shots or upload the plan to explain.depesz.com

– a_horse_with_no_name
Nov 22 '18 at 12:11












2 Answers
2






active

oldest

votes


















3














Try this one, Join query will enhance the indexing and optimized query execution time



SELECT id
FROM activitylog
WHERE url in ('/staff/save/117','/staff/create/117')
AND TIMESTAMP > '1990-01-01 00:00:00'
AND EXISTS
(SELECT 1
FROM users AS u
JOIN users ur ON ur.CompanyID = u.CompanyID
WHERE ur.ID = '150'
AND u.id = activitylog.userid)
ORDER BY TIMESTAMP DESC





share|improve this answer


























  • I tried it . But It shows error. ` column reference "id" is ambiguous`

    – Vi J
    Nov 22 '18 at 12:21











  • check now, i have updated the code with object ref.

    – Pranesh Janarthanan
    Nov 22 '18 at 12:22











  • Thanks for your quick reply. I tried the update one. But no improvement.

    – Vi J
    Nov 22 '18 at 12:26











  • If your activitylog table having millions of records, try lazy loading by processing limited records first. example: page scroller reaches end of the page then show some bunch records.

    – Pranesh Janarthanan
    Nov 22 '18 at 12:29






  • 1





    Good work improving the query. Now all that is left to do is to replace the IN (SELECT ...) with an EXISTS (SELECT ...) and add the appropriate indexes.

    – Laurenz Albe
    Nov 22 '18 at 12:39



















1














You should make sure you have the right indexes. Also, let's optimize the query:



SELECT id 
FROM activitylog
WHERE url in ('/staff/save/117', '/staff/create/117')
AND timestamp > '1990-01-01 00:00:00'
AND
(
(userid = 150) or
EXISTS
(
select 1
from users workmate
join users u150
on workmate.companyid = u150.companyid and u150.id = 150
and activitylog.userid = workmate.id
)
)
ORDER BY timestamp DESC





share|improve this answer


























  • @Vi J try this updated query and post the execution time taken for this. We are waiting eagerly to know the results.

    – Pranesh Janarthanan
    Nov 23 '18 at 6:08











  • It shows error column "user_id" does not exist

    – Vi J
    Nov 23 '18 at 6:43






  • 1





    @ViJ this concludes, you got the maximum optimized query.

    – Pranesh Janarthanan
    Nov 23 '18 at 10:49






  • 1





    @PraneshJanarthanan actually there is still a possibility to improve the query (besides improving the indexes, of course). One could run the equivalent of a subquery to get the ids and then replace the exists part with userid in (nr1, nr2, ..., nrn). This should greatly improve performance, since the set of ids does not depend on the activitylog record.

    – Lajos Arpad
    Nov 23 '18 at 11:52






  • 1





    @Vi J Show execution time of partial query: SELECT id FROM activitylog WHERE url in ('/staff/save/117', '/staff/create/117') AND timestamp > '1990-01-01 00:00:00' Also tell what (if any ) indexes did you set for mentioned tables ?

    – Vancalar
    Nov 23 '18 at 14:09











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%2f53430590%2freduce-the-execution-time-in-postgresql%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









3














Try this one, Join query will enhance the indexing and optimized query execution time



SELECT id
FROM activitylog
WHERE url in ('/staff/save/117','/staff/create/117')
AND TIMESTAMP > '1990-01-01 00:00:00'
AND EXISTS
(SELECT 1
FROM users AS u
JOIN users ur ON ur.CompanyID = u.CompanyID
WHERE ur.ID = '150'
AND u.id = activitylog.userid)
ORDER BY TIMESTAMP DESC





share|improve this answer


























  • I tried it . But It shows error. ` column reference "id" is ambiguous`

    – Vi J
    Nov 22 '18 at 12:21











  • check now, i have updated the code with object ref.

    – Pranesh Janarthanan
    Nov 22 '18 at 12:22











  • Thanks for your quick reply. I tried the update one. But no improvement.

    – Vi J
    Nov 22 '18 at 12:26











  • If your activitylog table having millions of records, try lazy loading by processing limited records first. example: page scroller reaches end of the page then show some bunch records.

    – Pranesh Janarthanan
    Nov 22 '18 at 12:29






  • 1





    Good work improving the query. Now all that is left to do is to replace the IN (SELECT ...) with an EXISTS (SELECT ...) and add the appropriate indexes.

    – Laurenz Albe
    Nov 22 '18 at 12:39
















3














Try this one, Join query will enhance the indexing and optimized query execution time



SELECT id
FROM activitylog
WHERE url in ('/staff/save/117','/staff/create/117')
AND TIMESTAMP > '1990-01-01 00:00:00'
AND EXISTS
(SELECT 1
FROM users AS u
JOIN users ur ON ur.CompanyID = u.CompanyID
WHERE ur.ID = '150'
AND u.id = activitylog.userid)
ORDER BY TIMESTAMP DESC





share|improve this answer


























  • I tried it . But It shows error. ` column reference "id" is ambiguous`

    – Vi J
    Nov 22 '18 at 12:21











  • check now, i have updated the code with object ref.

    – Pranesh Janarthanan
    Nov 22 '18 at 12:22











  • Thanks for your quick reply. I tried the update one. But no improvement.

    – Vi J
    Nov 22 '18 at 12:26











  • If your activitylog table having millions of records, try lazy loading by processing limited records first. example: page scroller reaches end of the page then show some bunch records.

    – Pranesh Janarthanan
    Nov 22 '18 at 12:29






  • 1





    Good work improving the query. Now all that is left to do is to replace the IN (SELECT ...) with an EXISTS (SELECT ...) and add the appropriate indexes.

    – Laurenz Albe
    Nov 22 '18 at 12:39














3












3








3







Try this one, Join query will enhance the indexing and optimized query execution time



SELECT id
FROM activitylog
WHERE url in ('/staff/save/117','/staff/create/117')
AND TIMESTAMP > '1990-01-01 00:00:00'
AND EXISTS
(SELECT 1
FROM users AS u
JOIN users ur ON ur.CompanyID = u.CompanyID
WHERE ur.ID = '150'
AND u.id = activitylog.userid)
ORDER BY TIMESTAMP DESC





share|improve this answer















Try this one, Join query will enhance the indexing and optimized query execution time



SELECT id
FROM activitylog
WHERE url in ('/staff/save/117','/staff/create/117')
AND TIMESTAMP > '1990-01-01 00:00:00'
AND EXISTS
(SELECT 1
FROM users AS u
JOIN users ur ON ur.CompanyID = u.CompanyID
WHERE ur.ID = '150'
AND u.id = activitylog.userid)
ORDER BY TIMESTAMP DESC






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 23 '18 at 8:38









Laurenz Albe

45.6k102748




45.6k102748










answered Nov 22 '18 at 12:13









Pranesh JanarthananPranesh Janarthanan

469516




469516













  • I tried it . But It shows error. ` column reference "id" is ambiguous`

    – Vi J
    Nov 22 '18 at 12:21











  • check now, i have updated the code with object ref.

    – Pranesh Janarthanan
    Nov 22 '18 at 12:22











  • Thanks for your quick reply. I tried the update one. But no improvement.

    – Vi J
    Nov 22 '18 at 12:26











  • If your activitylog table having millions of records, try lazy loading by processing limited records first. example: page scroller reaches end of the page then show some bunch records.

    – Pranesh Janarthanan
    Nov 22 '18 at 12:29






  • 1





    Good work improving the query. Now all that is left to do is to replace the IN (SELECT ...) with an EXISTS (SELECT ...) and add the appropriate indexes.

    – Laurenz Albe
    Nov 22 '18 at 12:39



















  • I tried it . But It shows error. ` column reference "id" is ambiguous`

    – Vi J
    Nov 22 '18 at 12:21











  • check now, i have updated the code with object ref.

    – Pranesh Janarthanan
    Nov 22 '18 at 12:22











  • Thanks for your quick reply. I tried the update one. But no improvement.

    – Vi J
    Nov 22 '18 at 12:26











  • If your activitylog table having millions of records, try lazy loading by processing limited records first. example: page scroller reaches end of the page then show some bunch records.

    – Pranesh Janarthanan
    Nov 22 '18 at 12:29






  • 1





    Good work improving the query. Now all that is left to do is to replace the IN (SELECT ...) with an EXISTS (SELECT ...) and add the appropriate indexes.

    – Laurenz Albe
    Nov 22 '18 at 12:39

















I tried it . But It shows error. ` column reference "id" is ambiguous`

– Vi J
Nov 22 '18 at 12:21





I tried it . But It shows error. ` column reference "id" is ambiguous`

– Vi J
Nov 22 '18 at 12:21













check now, i have updated the code with object ref.

– Pranesh Janarthanan
Nov 22 '18 at 12:22





check now, i have updated the code with object ref.

– Pranesh Janarthanan
Nov 22 '18 at 12:22













Thanks for your quick reply. I tried the update one. But no improvement.

– Vi J
Nov 22 '18 at 12:26





Thanks for your quick reply. I tried the update one. But no improvement.

– Vi J
Nov 22 '18 at 12:26













If your activitylog table having millions of records, try lazy loading by processing limited records first. example: page scroller reaches end of the page then show some bunch records.

– Pranesh Janarthanan
Nov 22 '18 at 12:29





If your activitylog table having millions of records, try lazy loading by processing limited records first. example: page scroller reaches end of the page then show some bunch records.

– Pranesh Janarthanan
Nov 22 '18 at 12:29




1




1





Good work improving the query. Now all that is left to do is to replace the IN (SELECT ...) with an EXISTS (SELECT ...) and add the appropriate indexes.

– Laurenz Albe
Nov 22 '18 at 12:39





Good work improving the query. Now all that is left to do is to replace the IN (SELECT ...) with an EXISTS (SELECT ...) and add the appropriate indexes.

– Laurenz Albe
Nov 22 '18 at 12:39













1














You should make sure you have the right indexes. Also, let's optimize the query:



SELECT id 
FROM activitylog
WHERE url in ('/staff/save/117', '/staff/create/117')
AND timestamp > '1990-01-01 00:00:00'
AND
(
(userid = 150) or
EXISTS
(
select 1
from users workmate
join users u150
on workmate.companyid = u150.companyid and u150.id = 150
and activitylog.userid = workmate.id
)
)
ORDER BY timestamp DESC





share|improve this answer


























  • @Vi J try this updated query and post the execution time taken for this. We are waiting eagerly to know the results.

    – Pranesh Janarthanan
    Nov 23 '18 at 6:08











  • It shows error column "user_id" does not exist

    – Vi J
    Nov 23 '18 at 6:43






  • 1





    @ViJ this concludes, you got the maximum optimized query.

    – Pranesh Janarthanan
    Nov 23 '18 at 10:49






  • 1





    @PraneshJanarthanan actually there is still a possibility to improve the query (besides improving the indexes, of course). One could run the equivalent of a subquery to get the ids and then replace the exists part with userid in (nr1, nr2, ..., nrn). This should greatly improve performance, since the set of ids does not depend on the activitylog record.

    – Lajos Arpad
    Nov 23 '18 at 11:52






  • 1





    @Vi J Show execution time of partial query: SELECT id FROM activitylog WHERE url in ('/staff/save/117', '/staff/create/117') AND timestamp > '1990-01-01 00:00:00' Also tell what (if any ) indexes did you set for mentioned tables ?

    – Vancalar
    Nov 23 '18 at 14:09
















1














You should make sure you have the right indexes. Also, let's optimize the query:



SELECT id 
FROM activitylog
WHERE url in ('/staff/save/117', '/staff/create/117')
AND timestamp > '1990-01-01 00:00:00'
AND
(
(userid = 150) or
EXISTS
(
select 1
from users workmate
join users u150
on workmate.companyid = u150.companyid and u150.id = 150
and activitylog.userid = workmate.id
)
)
ORDER BY timestamp DESC





share|improve this answer


























  • @Vi J try this updated query and post the execution time taken for this. We are waiting eagerly to know the results.

    – Pranesh Janarthanan
    Nov 23 '18 at 6:08











  • It shows error column "user_id" does not exist

    – Vi J
    Nov 23 '18 at 6:43






  • 1





    @ViJ this concludes, you got the maximum optimized query.

    – Pranesh Janarthanan
    Nov 23 '18 at 10:49






  • 1





    @PraneshJanarthanan actually there is still a possibility to improve the query (besides improving the indexes, of course). One could run the equivalent of a subquery to get the ids and then replace the exists part with userid in (nr1, nr2, ..., nrn). This should greatly improve performance, since the set of ids does not depend on the activitylog record.

    – Lajos Arpad
    Nov 23 '18 at 11:52






  • 1





    @Vi J Show execution time of partial query: SELECT id FROM activitylog WHERE url in ('/staff/save/117', '/staff/create/117') AND timestamp > '1990-01-01 00:00:00' Also tell what (if any ) indexes did you set for mentioned tables ?

    – Vancalar
    Nov 23 '18 at 14:09














1












1








1







You should make sure you have the right indexes. Also, let's optimize the query:



SELECT id 
FROM activitylog
WHERE url in ('/staff/save/117', '/staff/create/117')
AND timestamp > '1990-01-01 00:00:00'
AND
(
(userid = 150) or
EXISTS
(
select 1
from users workmate
join users u150
on workmate.companyid = u150.companyid and u150.id = 150
and activitylog.userid = workmate.id
)
)
ORDER BY timestamp DESC





share|improve this answer















You should make sure you have the right indexes. Also, let's optimize the query:



SELECT id 
FROM activitylog
WHERE url in ('/staff/save/117', '/staff/create/117')
AND timestamp > '1990-01-01 00:00:00'
AND
(
(userid = 150) or
EXISTS
(
select 1
from users workmate
join users u150
on workmate.companyid = u150.companyid and u150.id = 150
and activitylog.userid = workmate.id
)
)
ORDER BY timestamp DESC






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 23 '18 at 11:30

























answered Nov 22 '18 at 14:17









Lajos ArpadLajos Arpad

27.7k1861116




27.7k1861116













  • @Vi J try this updated query and post the execution time taken for this. We are waiting eagerly to know the results.

    – Pranesh Janarthanan
    Nov 23 '18 at 6:08











  • It shows error column "user_id" does not exist

    – Vi J
    Nov 23 '18 at 6:43






  • 1





    @ViJ this concludes, you got the maximum optimized query.

    – Pranesh Janarthanan
    Nov 23 '18 at 10:49






  • 1





    @PraneshJanarthanan actually there is still a possibility to improve the query (besides improving the indexes, of course). One could run the equivalent of a subquery to get the ids and then replace the exists part with userid in (nr1, nr2, ..., nrn). This should greatly improve performance, since the set of ids does not depend on the activitylog record.

    – Lajos Arpad
    Nov 23 '18 at 11:52






  • 1





    @Vi J Show execution time of partial query: SELECT id FROM activitylog WHERE url in ('/staff/save/117', '/staff/create/117') AND timestamp > '1990-01-01 00:00:00' Also tell what (if any ) indexes did you set for mentioned tables ?

    – Vancalar
    Nov 23 '18 at 14:09



















  • @Vi J try this updated query and post the execution time taken for this. We are waiting eagerly to know the results.

    – Pranesh Janarthanan
    Nov 23 '18 at 6:08











  • It shows error column "user_id" does not exist

    – Vi J
    Nov 23 '18 at 6:43






  • 1





    @ViJ this concludes, you got the maximum optimized query.

    – Pranesh Janarthanan
    Nov 23 '18 at 10:49






  • 1





    @PraneshJanarthanan actually there is still a possibility to improve the query (besides improving the indexes, of course). One could run the equivalent of a subquery to get the ids and then replace the exists part with userid in (nr1, nr2, ..., nrn). This should greatly improve performance, since the set of ids does not depend on the activitylog record.

    – Lajos Arpad
    Nov 23 '18 at 11:52






  • 1





    @Vi J Show execution time of partial query: SELECT id FROM activitylog WHERE url in ('/staff/save/117', '/staff/create/117') AND timestamp > '1990-01-01 00:00:00' Also tell what (if any ) indexes did you set for mentioned tables ?

    – Vancalar
    Nov 23 '18 at 14:09

















@Vi J try this updated query and post the execution time taken for this. We are waiting eagerly to know the results.

– Pranesh Janarthanan
Nov 23 '18 at 6:08





@Vi J try this updated query and post the execution time taken for this. We are waiting eagerly to know the results.

– Pranesh Janarthanan
Nov 23 '18 at 6:08













It shows error column "user_id" does not exist

– Vi J
Nov 23 '18 at 6:43





It shows error column "user_id" does not exist

– Vi J
Nov 23 '18 at 6:43




1




1





@ViJ this concludes, you got the maximum optimized query.

– Pranesh Janarthanan
Nov 23 '18 at 10:49





@ViJ this concludes, you got the maximum optimized query.

– Pranesh Janarthanan
Nov 23 '18 at 10:49




1




1





@PraneshJanarthanan actually there is still a possibility to improve the query (besides improving the indexes, of course). One could run the equivalent of a subquery to get the ids and then replace the exists part with userid in (nr1, nr2, ..., nrn). This should greatly improve performance, since the set of ids does not depend on the activitylog record.

– Lajos Arpad
Nov 23 '18 at 11:52





@PraneshJanarthanan actually there is still a possibility to improve the query (besides improving the indexes, of course). One could run the equivalent of a subquery to get the ids and then replace the exists part with userid in (nr1, nr2, ..., nrn). This should greatly improve performance, since the set of ids does not depend on the activitylog record.

– Lajos Arpad
Nov 23 '18 at 11:52




1




1





@Vi J Show execution time of partial query: SELECT id FROM activitylog WHERE url in ('/staff/save/117', '/staff/create/117') AND timestamp > '1990-01-01 00:00:00' Also tell what (if any ) indexes did you set for mentioned tables ?

– Vancalar
Nov 23 '18 at 14:09





@Vi J Show execution time of partial query: SELECT id FROM activitylog WHERE url in ('/staff/save/117', '/staff/create/117') AND timestamp > '1990-01-01 00:00:00' Also tell what (if any ) indexes did you set for mentioned tables ?

– Vancalar
Nov 23 '18 at 14:09


















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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53430590%2freduce-the-execution-time-in-postgresql%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

Wiesbaden

Marschland

Dieringhausen