query-optimization

Speed up plpgsql that counts doc types in a loop?

只谈情不闲聊 提交于 2019-12-02 02:58:33
Is there a way to speed up our plpgsql function that counts certain types of docs all in one query which is executed in a loop? ALL in one query? validador := (select count(id_doc) from webdte.doc_tip_cifra where id_doc = id_documento and id_tipo_cifra = 901); validador2 := (select count(id_doc) from webdte.doc_tip_cifra where id_doc = id_documento and id_tipo_cifra = 902); validador3 := (select count(id_doc) from webdte.doc_tip_cifra where id_doc = id_documento and id_tipo_cifra = 905); validador4 := (select count(id_doc) from webdte.doc_tip_cifra where id_doc = id_documento and id_tipo_cifra

Optimizing ORDER BY

孤街醉人 提交于 2019-12-02 00:24:30
问题 I am trying to optimize this query that sorts posts by reputation field (1st) and then id field (2nd). Without 1st field query takes ~0.250sec, but with it takes up to ~2.500sec (means 10x times slower, terrible). Any suggestion? SELECT -- everything is ok here FROM posts AS p ORDER BY -- 1st: sort by reputation if exists (1 reputation = 1 day) (CASE WHEN p.created_at >= unix_timestamp(now() - INTERVAL p.reputation DAY) THEN +p.reputation ELSE NULL END) DESC, -- also used 0 instead of NULL --

Efficiently joining over interval ranges in SQL

我只是一个虾纸丫 提交于 2019-12-01 23:42:08
问题 Suppose I have two tables as follows (data taken from this SO post): Table d1 : x start end a 1 3 b 5 11 c 19 22 d 30 39 e 7 25 Table d2 : x pos a 2 a 3 b 3 b 12 c 20 d 52 e 10 The first row in both tables are column headers. I'd like to extract all the rows in d2 where column x matches with d1 and pos1 falls within (including boundary values) d1 's start and end columns. That is, I'd like the result: x pos start end a 2 1 3 a 3 1 3 c 20 19 22 e 10 7 25 The way I've seen this done so far is:

Optimizing ORDER BY

点点圈 提交于 2019-12-01 22:34:47
I am trying to optimize this query that sorts posts by reputation field (1st) and then id field (2nd). Without 1st field query takes ~0.250sec, but with it takes up to ~2.500sec (means 10x times slower, terrible). Any suggestion? SELECT -- everything is ok here FROM posts AS p ORDER BY -- 1st: sort by reputation if exists (1 reputation = 1 day) (CASE WHEN p.created_at >= unix_timestamp(now() - INTERVAL p.reputation DAY) THEN +p.reputation ELSE NULL END) DESC, -- also used 0 instead of NULL -- 2nd: sort by id dec p.id DESC WHERE p.status = 'published' -- the only thing for filter LIMIT 0,10 --

Efficiently joining over interval ranges in SQL

╄→гoц情女王★ 提交于 2019-12-01 22:31:45
Suppose I have two tables as follows (data taken from this SO post ): Table d1 : x start end a 1 3 b 5 11 c 19 22 d 30 39 e 7 25 Table d2 : x pos a 2 a 3 b 3 b 12 c 20 d 52 e 10 The first row in both tables are column headers. I'd like to extract all the rows in d2 where column x matches with d1 and pos1 falls within (including boundary values) d1 's start and end columns. That is, I'd like the result: x pos start end a 2 1 3 a 3 1 3 c 20 19 22 e 10 7 25 The way I've seen this done so far is: SELECT * FROM d1 JOIN d2 USING (x) WHERE pos BETWEEN start AND end But what is not clear to me is if

MySQL query optimization of LIKE term% ORDER BY int

ε祈祈猫儿з 提交于 2019-12-01 21:14:28
My question is regarding the handling of MySQL index on VARCHAR combined with an int COLUMN when using prefix matching. e.g. if I have such a query: SELECT * FROM tbl WHERE name LIKE 'query%' ORDER BY weight DESC LIMIT 5 Considering I have one index one name->weight, does that index need to find all apperances of the prefix query and then ORDER BY, or does he keeps the cross calculation indexed even with the use of prefix matching (%). I'm troubled by it, because for popular names (e.g. query=john) I might find myself searching for a long time for all appearances of john, and that will make

Conditional counting: Performance differences in using SUM() vs COUNT()?

爱⌒轻易说出口 提交于 2019-12-01 19:46:29
问题 Just as a very simple example, let's say I have table test with sample data like so: a | b ------------- 1 | 18 1 | 24 1 | 64 1 | 82 1 | 10 1 | 7 2 | 5 2 | 18 2 | 66 2 | 72 3 | 81 3 | 97 And for each a , I'm to get the count of how many b 's there are that are < 50. The result would look like: a | bcnt -------------- 1 | 4 2 | 2 3 | 0 Now I could achieve this result in either of two ways: SELECT a, COUNT(CASE WHEN b < 50 THEN 1 ELSE NULL END) AS bcnt FROM test GROUP BY a Or: SELECT a, SUM

Is SQL DATEDIFF(year, …, …) an Expensive Computation?

*爱你&永不变心* 提交于 2019-12-01 18:42:13
I'm trying to optimize up some horrendously complicated SQL queries because it takes too long to finish. In my queries, I have dynamically created SQL statements with lots of the same functions, so I created a temporary table where each function is only called once instead of many, many times - this cut my execution time by 3/4. So my question is, can I expect to see much of a difference if say, 1,000 datediff computations are narrowed to 100? EDIT: The query looks like this : SELECT DISTINCT M.MID, M.RE FROM #TEMP INNER JOIN M ON #TEMP.MID=M.MID WHERE ( #TEMP.Property1=1 ) AND DATEDIFF( year,

Conditional counting: Performance differences in using SUM() vs COUNT()?

痴心易碎 提交于 2019-12-01 18:15:16
Just as a very simple example, let's say I have table test with sample data like so: a | b ------------- 1 | 18 1 | 24 1 | 64 1 | 82 1 | 10 1 | 7 2 | 5 2 | 18 2 | 66 2 | 72 3 | 81 3 | 97 And for each a , I'm to get the count of how many b 's there are that are < 50. The result would look like: a | bcnt -------------- 1 | 4 2 | 2 3 | 0 Now I could achieve this result in either of two ways: SELECT a, COUNT(CASE WHEN b < 50 THEN 1 ELSE NULL END) AS bcnt FROM test GROUP BY a Or: SELECT a, SUM(CASE WHEN b < 50 THEN 1 ELSE 0 END) AS bcnt FROM test GROUP BY a I know this may seem like such an

MySQL EXPLAIN 'type' changes from 'range' to 'ref' when the date in the where statement is changed?

南笙酒味 提交于 2019-12-01 18:13:35
I've been testing out different ideas for optimizing some of the tables we have in our system at work. Today I came across a table that tracks every view on each vehicle in our system. Create table below. SHOW CREATE TABLE vehicle_view_tracking; CREATE TABLE `vehicle_view_tracking` ( `vehicle_view_tracking_id` int(10) unsigned NOT NULL AUTO_INCREMENT, `public_key` varchar(45) NOT NULL, `vehicle_id` int(10) unsigned NOT NULL, `landing_url` longtext NOT NULL, `landing_port` int(11) NOT NULL, `http_referrer` longtext, `created_on` datetime NOT NULL, `created_on_date` date NOT NULL, `server_host`