query-optimization

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

旧街凉风 提交于 2019-11-27 19:36:56
问题 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

Is possible to reuse subqueries?

一世执手 提交于 2019-11-27 17:21:02
问题 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'

Role of selectivity in index scan/seek

淺唱寂寞╮ 提交于 2019-11-27 14:48:39
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? 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 unique the values, the higher the selectivity, which means that a unique index has the highest possible

Does MySQL eliminate common subexpressions between SELECT and HAVING/GROUP BY clause

烈酒焚心 提交于 2019-11-27 13:48:16
I often see people answer MySQL questions with queries like this: SELECT DAY(date), other columns FROM table GROUP BY DAY(date); SELECT somecolumn, COUNT(*) FROM table HAVING COUNT(*) > 1; I always like to give the column an alias and refer to that in the GROUP BY or HAVING clause, e.g. SELECT DAY(date) AS day, other columns FROM table GROUP BY day; SELECT somecolumn, COUNT(*) AS c FROM table HAVING c > 1; Is MySQL smart enough to notice that the expressions in the later clauses are the same as in SELECT , and only do it once? I'm not sure how to test this -- EXPLAIN doesn't show any

MySQL “IN” queries terribly slow with subquery but fast with explicit values

独自空忆成欢 提交于 2019-11-27 13:35:51
I have a MySQL query (Ubu 10.04,Innodb, Core i7, 16Gb RAM, SSD drives, MySQL params optimized): SELECT COUNT(DISTINCT subscriberid) FROM em_link_data WHERE linkid in (SELECT l.id FROM em_link l WHERE l.campaignid = '2900' AND l.link != 'open') The table em_link_data has about 7million rows, em_link has a few thousand. This query will take about 18 seconds to complete. However, if I substitute the results of the subquery and do this: SELECT COUNT(DISTINCT subscriberid) FROM em_link_data WHERE linkid in (24899,24900,24901,24902); then the query will run in less than 1 millisecond. The subquery

How to estimate SQL query timing?

感情迁移 提交于 2019-11-27 12:27:28
I'm trying to get an rough (order-of-magnitude) estimate of how long time the following query could take: mysql> EXPLAIN SELECT t1.col1, t1_col4 FROM t1 LEFT JOIN t2 ON t1.col1=t2.col1 WHERE col2=0 AND col3 IS NULL; +----+-------------+--------------------+------+---------------+------------+---------+-----------------------------+---------+--------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+--------------------+------+---------------+------------+---------+-----------------------------+---------+-------------

Avoid Naming User Stored Procedures SP% or SP_%

核能气质少年 提交于 2019-11-27 09:42:29
Does a user stored procedure with the prefix of SP (not SP_) have poorer performance by looking in the Master DB (similar to user SP named SP_) or does it look in the DB where the stored procedure exists because it lack an underscore? Martin Smith The reserved prefix that exhibits this behaviour is sp_ . Not sp . A stored procedure called spAddUser will be resolved in the normal way without looking for a matching object in master . The relevant quote in books online is Avoid the use of the sp_ prefix when naming procedures. This prefix is used by SQL Server to designate system procedures.

Does query plan optimizer works well with joined/filtered table-valued functions?

折月煮酒 提交于 2019-11-27 09:37:14
In SQLSERVER 2005, I'm using table-valued function as a convenient way to perform arbitrary aggregation on subset data from large table (passing date range or such parameters). I'm using theses inside larger queries as joined computations and I'm wondering if the query plan optimizer work well with them in every condition or if I'm better to unnest such computation in my larger queries. Does query plan optimizer unnest table-valued functions if it make sense? If it doesn't, what do you recommend to avoid code duplication that would occur by manually unnesting them? If it does, how do you

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

最后都变了- 提交于 2019-11-27 09:36:22
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 ------------------------------------------------------------------------------------------------------------------------------------------------

Does assigning stored procedure input parameters to local variables help optimize the query?

浪子不回头ぞ 提交于 2019-11-27 09:23:23
I have a stored procedure that takes 5 input parameters. The procedure is a bit complicated and takes around 2 minutes to execute. I am in process of optimizing query. So, my question is, does it always help to assign input parameters to local variables and then use local variables in the procedure? If so, how does it help? I will not try and explain the full details of parameter sniffing, but in short, no it does not always help (and it can hinder). Imagine a table (T) with a primary key and an indexed Date column (A), in the table there are 1,000 rows, 400 have the same value of A (lets say