query-optimization

Why is MAX() 100 times slower than ORDER BY … LIMIT 1?

爷,独闯天下 提交于 2019-11-30 13:52:19
I have a table foo with (among 20 others) columns bar , baz and quux with indexes on baz and quux . The table has ~500k rows. Why do the following to queries differ so much in speed? Query A takes 0.3s, while query B takes 28s. Query A select baz from foo where bar = :bar and quux = (select quux from foo where bar = :bar order by quux desc limit 1) Explain id select_type table type possible_keys key key_len ref rows Extra 1 PRIMARY foo ref quuxIdx quuxIdx 9 const 2 "Using where" 2 SUBQUERY foo index NULL quuxIdx 9 NULL 1 "Using where" Query B select baz from foo where bar = :bar and quux =

Mysql improve SELECT speed

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-30 12:42:24
I'm currently trying to improve the speed of SELECTS for a MySQL table and would appreciate any suggestions on ways to improve it. We have over 300 million records in the table and the table has the structure tag, date, value. The primary key is a combined key of tag and date. The table contains information for about 600 unique tags most containing an average of about 400,000 rows but can range from 2000 to over 11 million rows. The queries run against the table are: SELECT date, value FROM table WHERE tag = "a" AND date BETWEEN 'x' and 'y' ORDER BY date ....and there are very few if any

Increment counter or insert row in one statement, in SQLite

和自甴很熟 提交于 2019-11-30 11:31:49
In SQLite, given this database schema CREATE TABLE observations ( src TEXT, dest TEXT, verb TEXT, occurrences INTEGER ); CREATE UNIQUE INDEX observations_index ON observations (src, dest, verb); whenever a new observation tuple (:src, :dest, :verb) comes in, I want to either increment the "occurrences" column for the existing row for that tuple, or add a new row with occurrences=1 if there isn't already one. In concrete pseudocode: if (SELECT COUNT(*) FROM observations WHERE src == :src AND dest == :dest AND verb == :verb) == 1: UPDATE observations SET occurrences = occurrences + 1 WHERE src =

How to know which count query is the fastest?

烈酒焚心 提交于 2019-11-30 08:38:55
问题 I've been exploring query optimizations in the recent releases of Spark SQL 2.3.0-SNAPSHOT and noticed different physical plans for semantically-identical queries. Let's assume I've got to count the number of rows in the following dataset: val q = spark.range(1) I could count the number of rows as follows: q.count q.collect.size q.rdd.count q.queryExecution.toRdd.count My initial thought was that it's almost a constant operation (surely due to a local dataset) that would somehow have been

Does size of a VARCHAR column matter when used in queries [duplicate]

北城以北 提交于 2019-11-30 08:37:45
问题 This question already has answers here : Closed 6 years ago . Possible Duplicate: is there an advantage to varchar(500) over varchar(8000)? I understand that a VARCHAR(200) column containing 10 characters takes same amount of space as a VARCHAR(20) column containing same data. I want to know if changing a dozen VARCHAR(200) columns of a specific table to VARCHAR(20) would make the queries run faster, especially when: These columns will never contain more than 20 characters These columns are

Extremely slow PostgreSQL query with ORDER and LIMIT clauses

好久不见. 提交于 2019-11-30 07:54:39
I have a table, let's call it "foos", with almost 6 million records in it. I am running the following query: SELECT "foos".* FROM "foos" INNER JOIN "bars" ON "foos".bar_id = "bars".id WHERE (("bars".baz_id = 13266)) ORDER BY "foos"."id" DESC LIMIT 5 OFFSET 0; This query takes a very long time to run (Rails times out while running it). There is an index on all IDs in question. The curious part is, if I remove either the ORDER BY clause or the LIMIT clause, it runs almost instantaneously. I'm assuming that the presence of both ORDER BY and LIMIT are making PostgreSQL make some bad choices in

Bad optimization/planning on Postgres window-based queries (partition by(, group by?)) - 1000x speedup

偶尔善良 提交于 2019-11-30 07:39:21
We are running Postgres 9.3.5. (07/2014) We have quite some complex datawarehouse/reporting setup in place (ETL, materialized views, indexing, aggregations, analytical functions, ...). What I discovered right now may be difficult to implement in the optimizer (?), but it makes a huge difference in performance (only sample code with huge similarity to our query to reduce unnecessary complexity): create view foo as select sum(s.plan) over w_pyl as pyl_plan, -- money planned to spend in this pot/loc/year sum(s.booked) over w_pyl as pyl_booked, -- money already booked in this pot/loc/year -- money

are there any Query Optimization Tools for SQL Server? [closed]

為{幸葍}努か 提交于 2019-11-30 05:03:12
Are there any tools that do Query Optimizations for SQL Server 2005 and above? I've searched & didn't find anything. What do you recommend? If this Question was repeated before you can close it but I didn't see anything similar The best tool I've ever used for optimizing queries in MS SQL Server, by far, is the "Include Actual Execution Plan" option in Microsoft SQL Server Management Studio. It displays detailed information about the execution path taken by the server when running your query. (Note that this works best when there's data in the tables. Of course, without a good bit of test data

How to optimize an ORDER BY for a computed column on a MASSIVE MySQL table

ε祈祈猫儿з 提交于 2019-11-30 05:02:40
I have a very large (80+ million row) de-normalized MySQL table. A simplified schema looks like: +-----------+-------------+--------------+--------------+ | ID | PARAM1 | PARAM2 | PARAM3 | +-----------+-------------+--------------+--------------+ | 1 | .04 | .87 | .78 | +-----------+-------------+--------------+--------------+ | 2 | .12 | .02 | .76 | +-----------+-------------+--------------+--------------+ | 3 | .24 | .92 | .23 | +-----------+-------------+--------------+--------------+ | 4 | .65 | .12 | .01 | +-----------+-------------+--------------+--------------+ | 5 | .98 | .45 | .65 | +

LATERAL JOIN not using trigram index

ぃ、小莉子 提交于 2019-11-30 04:05:59
问题 I want to do some basic geocoding of addresses using Postgres. I have an address table that has around 1 million raw address strings: => \d addresses Table "public.addresses" Column | Type | Modifiers ---------+------+----------- address | text | I also have a table of location data: => \d locations Table "public.locations" Column | Type | Modifiers ------------+------+----------- id | text | country | text | postalcode | text | latitude | text | longitude | text | Most of the address strings