Optimizing (combining?) similar select queries
Clash 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?
sql postgresql
add a comment |Â
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?
sql postgresql
Can you explain in plain English what the second query is meant to accomplish?
â eurotrash
Jun 22 at 17:13
add a comment |Â
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?
sql postgresql
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?
sql postgresql
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
add a comment |Â
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
add a comment |Â
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Can you explain in plain English what the second query is meant to accomplish?
â eurotrash
Jun 22 at 17:13