query-optimization

How to optimize slow query with many joins [closed]

北城以北 提交于 2019-12-17 23:32:22
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed last year . 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

How can I analyse a Sqlite query execution?

こ雲淡風輕ζ 提交于 2019-12-17 21:50:10
问题 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? 回答1: 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

Creating a flattened table/view of a hierarchically-defined set of data

一个人想着一个人 提交于 2019-12-17 20:55:36
问题 I have a table containing hierarchical data. There are currently ~8 levels in this hierarchy. I really like the way the data is structured, but performance is dismal when I need to know if a record at level 8 is a child of a record at level 1. I have PL/SQL stored functions which do these lookups for me, each having a select * from tbl start with ... connect by... statement. This works fine when I'm querying a handful of records, but I'm in a situation now where I need to query ~10k records

How do I use DB2 Explain?

让人想犯罪 __ 提交于 2019-12-17 18:17:27
问题 How do I use DB2's Explain function? -- both to run it, and to use it to optimize queries. Is there a better tool available for DB2? I've built queries before, but the only way I've had to tell how long they'd take is to run them and time them -- which is hardly ideal. Edit: The answer for me turned out to be "You can't. You don't have and cannot get the access." Don't you love bureaucracy? 回答1: What you're looking for is covered by two DB2 utilities: The explain utility, which shows the

For autoincrement fields: MAX(ID) vs TOP 1 ID ORDER BY ID DESC

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-17 16:35:11
问题 I want to find the highest AutoIncremented value from a field. (its not being fetched after an insert where I can use @@SCOPE_IDENTITY etc) Which of these two queries would run faster or gives better performance. Id is the primary key and autoincrement field for Table1 . And this is for Sql Server 2005. SELECT MAX(Id) FROM Table1 SELECT TOP 1 Id FROM Table1 ORDER BY Id DESC [Edit] Yes in this case Id is the field on which I have defined the clustered index. If the index is ID DESC then what..

Role of selectivity in index scan/seek

≡放荡痞女 提交于 2019-12-17 12:35:28
问题 I have been reading in many SQL books and articles that selectivity is an important factor in creating index. If a column has low selectivity, an index seek does more harm that good. But none of the articles explain why. Can anybody explain why it is so, or provide a link to a relevant article? 回答1: From SimpleTalk article by Robert Sheldon: 14 SQL Server Indexing Questions You Were Too Shy To Ask The ratio of unique values within a key column is referred to as index selectivity. The more

How to avoid “Using temporary” in many-to-many queries?

守給你的承諾、 提交于 2019-12-17 09:59:48
问题 This query is very simple, all I want to do, is get all the articles in given category ordered by last_updated field: SELECT `articles`.* FROM `articles`, `articles_to_categories` WHERE `articles`.`id` = `articles_to_categories`.`article_id` AND `articles_to_categories`.`category_id` = 1 ORDER BY `articles`.`last_updated` DESC LIMIT 0, 20; But it runs very slow. Here is what EXPLAIN said: select_type table type possible_keys key key_len ref rows Extra -----------------------------------------

Optimize groupwise maximum query

て烟熏妆下的殇ゞ 提交于 2019-12-17 07:48:30
问题 select * from records where id in ( select max(id) from records group by option_id ) This query works fine even on millions of rows. However as you can see from the result of explain statement: QUERY PLAN ------------------------------------------------------------------------------------------------------------------------------------------- Nested Loop (cost=30218.84..31781.62 rows=620158 width=44) (actual time=1439.251..1443.458 rows=1057 loops=1) -> HashAggregate (cost=30218.41..30220.41

Whats the fastest way to lookup big tables for points within radius MySQL (latitude longitude)

拥有回忆 提交于 2019-12-17 06:55:05
问题 Currently I have a few tables with 100k+ rows. I am trying to lookup the data like follows. SELECT *, SQRT(POW(69.1 * (latitude - '49.1044302'), 2) + POW(69.1 * ('-122.801094' - longitude) * COS(latitude / 57.3), 2)) AS distance FROM stops HAVING distance < 5 ORDER BY distance limit 100 But currently this method slows with high load. Some queries are taking 20+ seconds to complete. If anyone knows any better ways to optimize this would be great. 回答1: Well first of all if you have a lot of

Optimize an ORDER BY query

若如初见. 提交于 2019-12-14 04:19:31
问题 I'm at a loss. I have a table with about 100K rows. When querying this table results are usually snappy, about 2ms or so. But whenever I use an ORDER BY performance drops like a rock to about 120ms. I read the MySQL ORDER BY Optimization page but I can't say I understand everything. Especially the indexes are unclear to me. Ultimately I would like to run the following query: SELECT * FROM `affiliate_new_contracts` WHERE phone_brand IN ('Apple','Blackberry','HTC','LG','Motorola','Nokia',