query-optimization

query efficiency - select the 2 latest “group/batch” records from table

落爺英雄遲暮 提交于 2019-12-12 01:25:42
问题 We have a tested a quite interesting SQL query. Unfortunately, It turned out that this query runs a little bit slow - O(n2) - and we are looking for a optimized solution or maybe also a totally different one? Goal: We would like to get for: - some customers ("record_customer_id"), e.g. ID 5 - the latest 2 "record_init_proc_id" - for every "record_inventory_id" http://www.sqlfiddle.com/#!9/07e5d/4 The query works fine and shows the correct results but uses at least two full table scans which

MYSQl Optimize Table Of Blog Posts With Comments

无人久伴 提交于 2019-12-12 01:18:54
问题 Im making a mysql table which will be holding my blog posts and some of their information. I want to be able to add comments the the blog posts and have them saved in mysql. My question is weather I should make a table called comments and have all the comments there have the blog post id, so I would only select the corresponding comments like this: select from comments where id = blogpostid Or my other idea was to put all the comments in an array, and save them in a longtext field in my blog

SQL query optimize

守給你的承諾、 提交于 2019-12-12 01:16:45
问题 In a slide http://www.slideshare.net/billkarwin/models-for-hierarchical-data, i saw a sql code (page 22): INSERT INTO Comments (author, comment) VALUES (‘Ollie’, ‘Good job!’); SELECT path FROM Comments WHERE comment_id = 7; UPDATE Comments SET path = $parent_path || LAST_INSERT_ID() || ‘/’ WHERE comment_id = LAST_INSERT_ID(); I think we can optimize it: SELECT path FROM Comments WHERE comment_id = 7; INSERT INTO Comments (author, comment, path ) VALUES (‘Ollie’, ‘Good job!’, $parent_path ||

Optimize performance of sub-queries

落花浮王杯 提交于 2019-12-12 00:52:29
问题 My first query from table takes about 40 seconds and creates over 80,000 rows. I want to get the counts of Windows 7 applications by Site, Sequence, Total and any OS version. These sub-queries work, but of course they slow the process down considerably. It took 3.5 hours to run. Is there a more efficient way to do this? Output: SoftwareName Sequence Site Win7/site Win7Installs/seq TotWin7apps TotalInstalls Adobe Acrobat 1 BKN 1 5 626 7854 AutoCAD LT 1 BKN 1 1 3 15 Adobe Acrobat 1 CTW 4 5 626

Optimize MySQL UPDATE query that contains WHERE and ORDER BY?

一世执手 提交于 2019-12-11 20:27:41
问题 How can I optimize this query? If I run it without the ORDER BY clause, it executes in <100ms. With the ORDER BY clause it takes many seconds, and crushes the server when more than one system is trying to make this query at once. UPDATE companies SET crawling = 1 WHERE crawling = 0 AND url_host IS NOT NULL ORDER BY last_crawled ASC LIMIT 1; If I run this query as a SELECT, it's also fast ( <100ms ). SELECT id FROM companies WHERE crawling = 0 AND url_host IS NOT NULL ORDER BY last_crawled ASC

Does SQL Server optimise constant expressions?

為{幸葍}努か 提交于 2019-12-11 19:32:21
问题 If I do something like this in SQL Server... select * from mytable where 1=1 ...does the 1=1 condition get optimised out, or is it actually evaluated for each row? I'd also be interested in knowing how other DBMSes behave in this regard, particularly MySQL. 回答1: SQL Server: First example: 1=1 predicate is automatically removed by SQL Server Query Optimizer because it's always true : Second example: Because 1=2 predicate is always false , the execution plan don't includes a data access

optimizing a union join inside select statement of other joins

ぃ、小莉子 提交于 2019-12-11 18:55:58
问题 I have a query I built in 3 -4 parts. This takes over 140secs to run once I add the union join with join. How can I change the union join to execute it faster. SELECT testing.CLIENTID, testing.COMPANY, testing.CONTACT, testing.CONTACTID, `orders`.`ORDERNO` AS `ORDERNO`, `orders`.`BIDNO` AS `BIDNO`, `projects`.`PROJID` AS `PROJID`, `projects`.`PROJCODE` AS `PROJCODE`, `projects`.`StartDate` AS `StartDate`, `category`.`type` AS `CATEGORY`, `projects`.`country` AS `COUNTRY`, `projects`.`VALUE`

Which query is faster?

微笑、不失礼 提交于 2019-12-11 17:49:37
问题 I get two answers: 1, 2. But I still do not know which is efficient. Here is explain of both: explain for my query: Hash Anti Join (cost=136.09..138.49 rows=1 width=556) Hash Cond: (t1.id = t2.parent_id) CTE tree -> Recursive Union (cost=0.14..132.81 rows=101 width=556) -> Index Scan using resource_type_idx_parent_resource_type_id on resource_type (cost=0 Index Cond: (parent_resource_type_id IS NULL) -> Hash Join (cost=0.33..12.26 rows=10 width=556) Hash Cond: (rt.parent_resource_type_id =

Queries in mysql 5.7 2 or more times slower than in 5.1

孤街浪徒 提交于 2019-12-11 17:49:15
问题 We migrated a full database from MySQL 5.1.63 to a different (a bit better) server into MySQL 5.7.22. Now most of the queries are 2 times slower on average. We haven't done too much optimization for MySQL 5.1. Here is the configuration which we have changed: table_open_cache = 4096 tmp_table_size=256M max_heap_table_size=256M query_cache_limit = 1000000 query_cache_size = 32000000 innodb_buffer_pool_size = 3200M innodb_log_buffer_size = 1024M Here is one concrete example: I want to get all

Optimal Oracle SQL Query to complete group-by on multiple columns in single table containing ~ 7,000,000 records

百般思念 提交于 2019-12-11 17:35:50
问题 I am a SQL Novice in need of some advice. What is the most efficient (fastest running query) way to do the following- Select all columns from a table after- -Performing a "Group By" based on the unique values contained in two columns: "top_line_id" and "external_reference". -Selecting a single record from each group based on the max or min value (doesn't matter which one) contained in a different field such as support_id. Someone on my team provided the below query, but I can't seem to get it