query-optimization

How can I force a subquery to perform as well as a #temp table?

徘徊边缘 提交于 2019-11-29 05:28:57
I am re-iterating the question asked by Mongus Pong Why would using a temp table be faster than a nested query? which doesn't have an answer that works for me. Most of us at some point find that when a nested query reaches a certain complexity it needs to broken into temp tables to keep it performative. It is absurd that this could ever be the most practical way forward and means these processes can no longer be made into a view. And often 3rd party BI apps will only play nicely with views so this is crucial. I am convinced there must be a simple queryplan setting to make the engine just spool

Is possible to reuse subqueries?

不羁岁月 提交于 2019-11-29 03:06:04
I'm having some problems trying to perform a query. I have two tables, one with elements information, and another one with records related with the elements of the first table. The idea is to get in the same row the element information plus several records information. Structure could be explain like this: table [ id, name ] [1, '1'], [2, '2'] table2 [ id, type, value ] [1, 1, '2009-12-02'] [1, 2, '2010-01-03'] [1, 4, '2010-01-03'] [2, 1, '2010-01-02'] [2, 2, '2010-01-02'] [2, 2, '2010-01-03'] [2, 3, '2010-01-07'] [2, 4, '2010-01-07'] And this is want I would like to achieve: result [id, name,

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

泪湿孤枕 提交于 2019-11-29 02:43:34
问题 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

MySQL Join clause vs WHERE clause

牧云@^-^@ 提交于 2019-11-29 01:29:36
What's the difference in a clause done the two following ways? SELECT * FROM table1 INNER JOIN table2 ON ( table2.col1 = table1.col2 AND table2.member_id = 4 ) I've compared them both with basic queries and EXPLAIN EXTENDED and don't see a difference. I'm wondering if someone here has discovered a difference in a more complex/processing intensive envornment. SELECT * FROM table1 INNER JOIN table2 ON ( table2.col1 = table1.col2 ) WHERE table2.member_id = 4 With an INNER join the two approaches give identical results and should produce the same query plan. However there is a semantic difference

What is the optimal way to compare dates in Microsoft SQL server?

半城伤御伤魂 提交于 2019-11-28 22:29:38
问题 I have a SQL datetime field in a very large table. It's indexed and needs to be queried. The problem is that SQL always stores the time component (even though it's always midnight), but the searches are to the day, rather than time. declare @dateVar datetime = '2013-03-11; select t.[DateColumn] from MyTable t where t.[DateColumn] = dateVar; Won't return anything, as the t.[DateColumn] always includes a time component. My question is what is the best way round this? There seem to be two main

Fastest way to find string by substring in SQL?

≯℡__Kan透↙ 提交于 2019-11-28 21:15:30
I have huge table with 2 columns: Id and Title. Id is bigint and I'm free to choose type of Title column: varchar, char, text, whatever. Column Title contains random text strings like "abcdefg", "q", "allyourbasebelongtous" with maximum of 255 chars. My task is to get strings by given substring. Substrings also have random length and can be start, middle or end of strings. The most obvious way to perform it: SELECT * FROM t LIKE '%abc%' I don't care about INSERT, I need only to do fast selects. What can I do to perform search as fast as possible? I use MS SQL Server 2008 R2, full text search

How to optimize slow query with many joins [closed]

南笙酒味 提交于 2019-11-28 20:31:29
My situation: the query searches around 90,000 vehicles the query takes long each time I already have indexes on all the fields being JOINed. How can I optimise it? Here is the query: SELECT vehicles.make_id, vehicles.fuel_id, vehicles.body_id, vehicles.transmission_id, vehicles.colour_id, vehicles.mileage, vehicles.vehicle_year, vehicles.engine_size, vehicles.trade_or_private, vehicles.doors, vehicles.model_id, Round(3959 * Acos(Cos(Radians(51.465436)) * Cos(Radians(vehicles.gps_lat)) * Cos( Radians(vehicles.gps_lon) - Radians( -0.296482)) + Sin( Radians(51.465436)) * Sin( Radians(vehicles

Rails 3 Database Indexes and other Optimization

余生颓废 提交于 2019-11-28 16:31:24
问题 I have been building rails apps for a while now, but unfortunately for me, none of my apps have had a large amount of data or traffic. But now I have one that is gaining steam. So I am diving in head first into scaling and optimizing my app. It seems the first and easiest step to do this is with database indexes. I've got a good huge list of indexes that should cover pretty much all of my queries, but when I added them to my database via migrations it only took a few seconds to add them. For

How can I analyse a Sqlite query execution?

泄露秘密 提交于 2019-11-28 16:24:52
I have a Sqlite database which I want to check the indexes are correct. MS SQL Analyser is great at breaking down the query execution and utilised indexes. Is there a similar tool for Sqlite? I know of no pretty graphical tools, but all of the information you seek is available from the EXPLAIN keyword. Consider this database: sqlite> create table users (name, email); sqlite> create index user_names on users (name); A query predicated on email will not use an index: sqlite> explain select * from users where email='foo'; 0|Trace|0|0|0||00| 1|String8|0|1|0|foo|00| 2|Goto|0|13|0||00| 3|OpenRead|0

Can the LIKE statement be optimized to not do full table scans?

﹥>﹥吖頭↗ 提交于 2019-11-28 14:18:24
I want to get a subtree from a table by tree path. the path column stores strings like: foo/ foo/bar/ foo/bar/baz/ If I try to select all records that start with a certain path: EXPLAIN QUERY PLAN SELECT * FROM f WHERE path LIKE "foo/%" it tells me that the table is scanned, even though the path column is indexed :( Is there any way I could make LIKE use the index and not scan the table? I found a way to achieve what I want with closure table, but it's harder to maintain and writes are extremely slow... To be able to use an index for LIKE in SQLite, the table column must have TEXT affinity , i