Optimizing (combining?) similar select queries

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
0
down vote

favorite












I have a table:



games (
appid integer PRIMARY KEY,
name text,
rating real,
votes integer,
score real,
windows boolean,
mac boolean,
linux boolean,
vr boolean,
release date,
price real,
tags text)


and a view on that table:



top_games AS 
SELECT *
FROM games
ORDER BY score DESC;


I also have indexes on the score, release, price, and tags columns.



The table is updated rarely (once a day), so I hope having this many indexes is fine.



What I'm mainly looking to optimize are my select statements. The [ WHERE expressions] part is created from user input, and is the same for each query. It feels like I wouldn't need to create the same query three times over, but I also can't seem to figure out a faster way to combine them into one.



SELECT COUNT(*) FROM games[ WHERE expressions];

SELECT row_number FROM (SELECT name, row_number() OVER() FROM top_games[ WHERE expressions]) AS win WHERE name ILIKE '%name%';

SELECT * FROM top_games[ WHERE expressions] LIMIT 25 OFFSET offset;


Any other optimizations that you could suggest are welcome too - I'm fairly new to writing SQL code, and there's probably lots to be done. Maybe some way to also index the name column so that it would actually be used?







share|improve this question





















  • Can you explain in plain English what the second query is meant to accomplish?
    – eurotrash
    Jun 22 at 17:13
















up vote
0
down vote

favorite












I have a table:



games (
appid integer PRIMARY KEY,
name text,
rating real,
votes integer,
score real,
windows boolean,
mac boolean,
linux boolean,
vr boolean,
release date,
price real,
tags text)


and a view on that table:



top_games AS 
SELECT *
FROM games
ORDER BY score DESC;


I also have indexes on the score, release, price, and tags columns.



The table is updated rarely (once a day), so I hope having this many indexes is fine.



What I'm mainly looking to optimize are my select statements. The [ WHERE expressions] part is created from user input, and is the same for each query. It feels like I wouldn't need to create the same query three times over, but I also can't seem to figure out a faster way to combine them into one.



SELECT COUNT(*) FROM games[ WHERE expressions];

SELECT row_number FROM (SELECT name, row_number() OVER() FROM top_games[ WHERE expressions]) AS win WHERE name ILIKE '%name%';

SELECT * FROM top_games[ WHERE expressions] LIMIT 25 OFFSET offset;


Any other optimizations that you could suggest are welcome too - I'm fairly new to writing SQL code, and there's probably lots to be done. Maybe some way to also index the name column so that it would actually be used?







share|improve this question





















  • Can you explain in plain English what the second query is meant to accomplish?
    – eurotrash
    Jun 22 at 17:13












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have a table:



games (
appid integer PRIMARY KEY,
name text,
rating real,
votes integer,
score real,
windows boolean,
mac boolean,
linux boolean,
vr boolean,
release date,
price real,
tags text)


and a view on that table:



top_games AS 
SELECT *
FROM games
ORDER BY score DESC;


I also have indexes on the score, release, price, and tags columns.



The table is updated rarely (once a day), so I hope having this many indexes is fine.



What I'm mainly looking to optimize are my select statements. The [ WHERE expressions] part is created from user input, and is the same for each query. It feels like I wouldn't need to create the same query three times over, but I also can't seem to figure out a faster way to combine them into one.



SELECT COUNT(*) FROM games[ WHERE expressions];

SELECT row_number FROM (SELECT name, row_number() OVER() FROM top_games[ WHERE expressions]) AS win WHERE name ILIKE '%name%';

SELECT * FROM top_games[ WHERE expressions] LIMIT 25 OFFSET offset;


Any other optimizations that you could suggest are welcome too - I'm fairly new to writing SQL code, and there's probably lots to be done. Maybe some way to also index the name column so that it would actually be used?







share|improve this question













I have a table:



games (
appid integer PRIMARY KEY,
name text,
rating real,
votes integer,
score real,
windows boolean,
mac boolean,
linux boolean,
vr boolean,
release date,
price real,
tags text)


and a view on that table:



top_games AS 
SELECT *
FROM games
ORDER BY score DESC;


I also have indexes on the score, release, price, and tags columns.



The table is updated rarely (once a day), so I hope having this many indexes is fine.



What I'm mainly looking to optimize are my select statements. The [ WHERE expressions] part is created from user input, and is the same for each query. It feels like I wouldn't need to create the same query three times over, but I also can't seem to figure out a faster way to combine them into one.



SELECT COUNT(*) FROM games[ WHERE expressions];

SELECT row_number FROM (SELECT name, row_number() OVER() FROM top_games[ WHERE expressions]) AS win WHERE name ILIKE '%name%';

SELECT * FROM top_games[ WHERE expressions] LIMIT 25 OFFSET offset;


Any other optimizations that you could suggest are welcome too - I'm fairly new to writing SQL code, and there's probably lots to be done. Maybe some way to also index the name column so that it would actually be used?









share|improve this question












share|improve this question




share|improve this question








edited May 24 at 12:39









Dmitry

12516




12516









asked May 24 at 11:21









Torn

113




113











  • Can you explain in plain English what the second query is meant to accomplish?
    – eurotrash
    Jun 22 at 17:13
















  • Can you explain in plain English what the second query is meant to accomplish?
    – eurotrash
    Jun 22 at 17:13















Can you explain in plain English what the second query is meant to accomplish?
– eurotrash
Jun 22 at 17:13




Can you explain in plain English what the second query is meant to accomplish?
– eurotrash
Jun 22 at 17:13















active

oldest

votes











Your Answer




StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");

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: "196"
;
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',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f195080%2foptimizing-combining-similar-select-queries%23new-answer', 'question_page');

);

Post as a guest



































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes










 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f195080%2foptimizing-combining-similar-select-queries%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

Greedy Best First Search implementation in Rust

Function to Return a JSON Like Objects Using VBA Collections and Arrays

C++11 CLH Lock Implementation