Infrequent SQL developers often get confused about when to put parentheses and/or aliases on derived tables. There has been this recent Reddit discussion about the subject, where user Elmhurstlol was wondering why they needed to provide an alias to the derived table (the subselect with the UNION) in the following query: SELECT AVG(price) AS AVG_PRICE … Continue reading Should I Put That Table Alias or Not? →
If you have been using certain DBMSs, or reading recent versions
of the SQL standard, you are probably aware of the so-called
“WITH clause” of SQL.
Some call it Subquery Factoring. Others call
it Common Table Expression. A form of the WITH
CLAUSE, “WITH RECURSIVE”, allows to design a
recursive query: a query which repeats itself again and again,
each time using the results of the previous iteration. This can
be quite useful to produce reports based on hierarchical data.
And thus is an alternative to Oracle’s CONNECT BY. MySQL does not
natively support WITH RECURSIVE, but it is easy to emulate it
with a generic, reusable stored procedure. Read the full article
…
[2017 update: MySQL 8.0.1 now features SQL-standard CTE
syntax; more information is here ; the entry below, from 2013, shows how
to work around the absence of CTEs in older MySQL
versions.]
If you have been using certain DBMSs, or reading recent versions
of the SQL standard, you are probably aware of the so-called
"WITH clause" of SQL.
Some call it Subquery Factoring. Others call
it Common Table Expression (CTE). In its simplest
form, this feature is a kind of "boosted derived table".
Assume that a table T1 has three columns:
CREATE TABLE T1( YEAR …[Read more]
Join 8000 others and follow Sean Hull on twitter @hullsean. SQL is derided by many and for good reason. It’s key to scalability yet terribly difficult to write good code. Here’s a few quick tips to write tighter queries in MySQL 1. Get rid of those Subqueries! Subqueries are a standard part of SQL, unfortunately […]
The post 3 Simple Patterns for Tighter MySQL Code appeared first on Scalable Startups.
There are so many exciting new features in MySQL 5.6 that I almost don't know where to start. To mention a few, MySQL's multi-core scalability has been significantly improved to meet modern hardware, InnoDB has better index statistics, much better performance, and online ALTER, replication has multi-threaded slaves and …
[Read more]
I’m very glad to announce you that MyXplain is released today.
This project is the result of many weeks of work for my friend Max and I and we are obviously very proud to present it.
Please take a time to browse it and find all the surprises that awaiting you.
We wanted to make an unique place, a gathering place where you
can find all you want to know about the most useful MySQL
commands.
We started with the explain command because we consider
it as one of the most important.
show processlist and show slave status will be
the next proposed commands on MyXplain.
The concept is pretty simple, we would like to offer you a new documentation to access all needed …
[Read more]
In a previous post, I had demonstrated how
subquery materialization, introduced in
MySQL 5.6.5, improves the performance of certain queries, like
query Q16 of DBT3. Such improvement was easily explained:
- Subquery materialization has a high start up cost (it needs to create and fill the temporary table).
- But afterwards it has fast lookups (temporary table has a hash index, no duplicates, and is in memory).
- In other words, compared to EXISTS, the first evaluation of the IN predicate is slow (high start up cost) and all following evaluations are fast (just a hash lookup).
- In the DBT 3 setup, one outer table (named "part") has 200,000 rows, so there are 200,000 evaluations of …
In a previous post, I analyzed how a query of the
famous DBT3 benchmark was
optimized by MySQL. It was this query, named "Q16" in the DBT3
jargon:
select p_brand, p_type, p_size, count(distinct ps_suppkey) as supplier_cnt from partsupp, part where p_partkey = ps_partkey and p_brand <> 'Brand#23' and p_type not like 'LARGE PLATED%' and p_size in (43, 1, 25, 5, 35, 12, 42, 40) and ps_suppkey not in ( select s_suppkey from supplier where s_comment like '%Customer%Complaints%' ) group by p_brand, p_type, p_size order by supplier_cnt desc, p_brand, p_type, p_size;
Here is a brief recap of conclusions I had drawn:
- for this query, MySQL tranforms the IN condition to EXISTS
and then
evaluates it with the "unique_subquery" technique, which does an …
MySQL version 4.1 was quite revolutionary. The main reason for that was support for sub-queries.1
However since then MySQL users were rather discouraged to use that functionality, basically due to the implementation’s poor performance and forced to build complicated queries based on joins rather than on subqueries.
Of course you can do some effort to optimize your subquery with sometimes very good results2. Not always it’s easy or even possible if you can’t change the code though.
You’d say it’s not a problem for typical OLTP, web based traffic at all, just don’t use subqueries! That’s true, …
[Read more]
If you use the EXPLAIN SELECT statement to see how your
subqueries are treated by MySQL, you may sometimes meet the
"unique_subquery" optimization. Here is how the manual describes it:
"unique_subquery: this type replaces ref for some
IN subqueries of the following form: value IN (SELECT
primary_key FROM single_table WHERE some_expr);
unique_subquery is just an index lookup function that replaces
the subquery completely for better efficiency".Few weeks ago,
while I was reviewing a patch fixing a bug in unique_subquery, I got a
"simplification" pulsion. I told myself that:
- unique_subquery is an optimization for a special case of simple subqueries (single inner …