query-optimization

MySQL query optimization and EXPLAIN for a noob

旧时模样 提交于 2019-12-20 05:36:29
问题 I've been working with databases for a long time but I'm new to query optimization. I have the following query (some of it code-generated): SELECT DISTINCT COALESCE(gi.start_time, '') start_time, COALESCE(b.name, '') bank, COALESCE(a.id, '') account_id, COALESCE(a.account_number, '') account_number, COALESCE(at.code, '') account_type, COALESCE(a.open_date, '') open_date, COALESCE(a.interest_rate, '') interest_rate, COALESCE(a.maturity_date, '') maturity_date, COALESCE(a.opening_balance, '')

How to optimize query if table contain 10000 entries using MySQL?

China☆狼群 提交于 2019-12-20 05:27:48
问题 When I execute this query like this they take so much execution time because user_fans table contain 10000 users entries. How can I optimize it? Query SELECT uf.`user_name`,uf.`user_id`, @post := (SELECT COUNT(*) FROM post WHERE user_id = uf.`user_id`) AS post, @post_comment_likes := (SELECT COUNT(*) FROM post_comment_likes WHERE user_id = uf.`user_id`) AS post_comment_likes, @post_comments := (SELECT COUNT(*) FROM post_comments WHERE user_id = uf.`user_id`) AS post_comments, @post_likes :=

MySQL query optimization of LIKE term% ORDER BY int

白昼怎懂夜的黑 提交于 2019-12-20 02:26:20
问题 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

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

不打扰是莪最后的温柔 提交于 2019-12-19 21:28:36
问题 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

Searching for a specific ID in a large database?

我的未来我决定 提交于 2019-12-19 11:39:09
问题 I need to look up an ID in a very large database. The ID is: 0167a901-e343-4745-963c-404809b74dd9 The database has hundreds of tables, and millions of rows in the big tables. I can narrow the date to within the last 2 or 3 months, but that's about it. I'm looking for any clues as to how to narrow down searches like this. One thing I'm curious about is whether using LIKE searches helps. i.e does it help to do something like select top 10 * from BIG_TABLE where DESIRED_ID like '016%' Any tips

When to use Oracle hints?

南楼画角 提交于 2019-12-19 10:27:48
问题 I'm doing some refactoring on a Oracle Schema (oracle version 10), and I see a lot of views that use hints *+ALL_ROWS*/ . In others views there are also other kind of hints. Why I should use an hints? the DB doesn't make the best choice in base of the query? many thanks! 回答1: That's a good question, but there's no single answer to it because there are different categories of hint for which different advice would apply. http://docs.oracle.com/cd/E11882_01/server.112/e16638/hintsref.htm

How to optimize database this query in large database?

十年热恋 提交于 2019-12-19 09:13:06
问题 Query SELECT id FROM `user_tmp` WHERE `code` = '9s5xs1sy' AND `go` NOT REGEXP 'http://www.xxxx.example.com/aflam/|http://xx.example.com|http://www.xxxxx..example.com/aflam/|http://www.xxxxxx.example.com/v/|http://www.xxxxxx.example.com/vb/' AND check='done' AND `dataip` <1319992460 ORDER BY id DESC LIMIT 50 MySQL returns: Showing rows 0 - 29 ( 50 total, Query took 21.3102 sec) [id: 2622270 - 2602288] Query took 21.3102 sec if i remove AND dataip <1319992460 MySQL returns Showing rows 0 - 29 (

Optimal query to fetch a cumulative sum in MySQL

China☆狼群 提交于 2019-12-19 05:57:14
问题 What is 'correct' query to fetch a cumulative sum in MySQL? I've a table where I keep information about files, one column list contains the size of the files in bytes. (the actual files are kept on disk somewhere) I would like to get the cumulative file size like this: +------------+---------+--------+----------------+ | fileInfoId | groupId | size | cumulativeSize | +------------+---------+--------+----------------+ | 1 | 1 | 522120 | 522120 | | 2 | 2 | 316042 | 316042 | | 4 | 2 | 711084 |

Optimize SQL that uses between clause

左心房为你撑大大i 提交于 2019-12-19 05:31:14
问题 Consider the following 2 tables: Table A: id event_time Table B id start_time end_time Every record in table A is mapped to exactly 1 record in table B. This means table B has no overlapping periods. Many records from table A can be mapped to the same record in table B. I need a query that returns all A.id, B.id pairs. Something like: SELECT A.id, B.id FROM A, B WHERE A.event_time BETWEEN B.start_time AND B.end_time I am using MySQL and I cannot optimize this query. With ~980 records in table

Optimize SQL that uses between clause

偶尔善良 提交于 2019-12-19 05:30:12
问题 Consider the following 2 tables: Table A: id event_time Table B id start_time end_time Every record in table A is mapped to exactly 1 record in table B. This means table B has no overlapping periods. Many records from table A can be mapped to the same record in table B. I need a query that returns all A.id, B.id pairs. Something like: SELECT A.id, B.id FROM A, B WHERE A.event_time BETWEEN B.start_time AND B.end_time I am using MySQL and I cannot optimize this query. With ~980 records in table