In my recent SQL work for a large Swiss bank, I have maintained nested database view monsters whose unnested SQL code amounted up to 5k lines of code, joining the same table over and over again in separate subselects combined via UNION operations. This monster performed in way under 50ms, no matter how we queried … Continue reading The Myth About Slow SQL JOIN Operations →
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.
This is the third blog post in the series of blog posts leading up to the talk comparing the optimizer enhancements in MySQL 5.6 and MariaDB 5.5. This blog post is targeted at the join related optimizations introduced in the optimizer. These optimizations are available in both MySQL 5.6 and MariaDB 5.5, and MariaDB 5.5
The post Join Optimizations in MySQL 5.6 and MariaDB 5.5 appeared first on ovais.tariq.
A lot has happened in MySQL 5.6 for queries joining many tables.
For the most common use cases we have drastically reduced the
cost of finding the execution plan. We have also improved the
heuristics and removed bugs so that the final plan is often
better than it used to be. Read on if you are one of those people
who do 15 way joins!
Finding a query execution plan
First some background. You can skip this part if you know how
MySQL picks the table join order in 5.5.
When presented with a query, MySQL will try to find the best
order to join tables by employing a greedy search algorithm. The
outcome is what we call a query execution plan, QEP. When you
join just a few tables, there's no problem calculating the cost
of all join order combinations and then pick the best plan.
However, since there are (#tables)! possible combinations, the
cost of calculating them all soon becomes too high: for five
tables, e.g., …
A very useful helper in your join toolbox can be a delete join. Even though it’s not a special join type but a join used within DELETE statements, it’s still worth mentioning. However, from time to time when I want to make use of delete joins on my own, I somehow managed it to forgot the syntax and have to look it up somewhere. Therefor, here is a description as well as an example.
Take care: A delete join is a very powerful weapon. And with great power comes great responsibility! I recommend to develop the delete join on a development database. At least, make sure you have a working an recent backup before trying to delete things. A delete statement that uses joins makes it easy to shot yourself in the foot. And if you do, it probably blows away your hole leg.
DELETE join syntax
When you …
[Read more]Getting two sets of information from one table in a select statement often leads people to write subselects, but it really doesn't matter that this is the same table twice, we can just give it a new alias and treat it as if it were a different table. This is one of those techniques where, once you've seen it, it's really obvious, but until that point it can be very confusing. I explained this to someone else recently, so I thought I'd capture it here in case it's helpful to anyone else.
Consider that tried-and-tested example: employees and managers.
Here's the staff
table from the database (today's
imaginary data isn't particularly imaginative, sorry):
mysql> select * from staff; +----+------------+-----------+------------+ | id | first_name | last_name | manager_id | +----+------------+-----------+------------+ | 1 | Hattie | Hopkins | 4 | | 2 | Henry | Hopkins | 4 | | 3 | Harry | …[Read more]
Getting two sets of information from one table in a select statement often leads people to write subselects, but it really doesn’t matter that this is the same table twice, we can just give it a new alias and treat it as if it were a different table. This is one of those techniques where, once you’ve seen it, it’s really obvious, but until that point it can be very confusing. I explained this to someone else recently, so I thought I’d capture it here in case it’s helpful to anyone else.
Consider that tried-and-tested example: employees and managers.
Here’s the staff
table from the database (today’s
imaginary data isn’t particularly imaginative, sorry):
mysql> select * from staff; +----+------------+-----------+------------+ | id | first_name | last_name | manager_id | +----+------------+-----------+------------+ | 1 | Hattie | Hopkins | 4 | | 2 | Henry | Hopkins | 4 | | 3 | …[Read more]
70x faster joins with AQL
The new GA MySQL Cluster 7.2 Release (7.2.4) just announced by Oracle includes 2 new features which when combined can improve the performance of joins by a factor of 70x (or even higher). The first enhancement is that MySQL Cluster now provides the MySQL Server with better information on the available indexes which allows the MySQL optimizer to automatically produce better query execution plans. Previously it was up to the user to manually provide hints to the optimizer. The second new feature is Adaptive Query Localization which allows the work of the join to be distributed across the data nodes (local to the data it’s working with) rather than up in the MySQL Server; this allows more computing power to be applied to calculating the join as well as dramatically reducing the number of messages …
[Read more]
I recently wrote a post about inner and outer joins, and a couple of people
asked what the difference is between USING
and
ON
.
In a nutshell, you use ON
for most things, but
USING
is a handy shorthand for the situation where
the column names are the same.
Consider this example dataset:
mysql> select * from pets; +---------+---------+--------+-----------+ | pets_id | animal | name | owners_id | +---------+---------+--------+-----------+ | 1 | fox | Rusty | 2 | | 2 | cat | Fluffy | 2 | | 3 | cat | Smudge | 3 | | 4 | cat | Toffee | 3 | | 5 | dog | Pig | 3 | | 6 | hamster | Henry | 1 | | 7 | dog | Honey | 1 | …[Read more]
I recently wrote a post about inner and outer joins, and a couple of people
asked what the difference is between USING
and
ON
.
In a nutshell, you use ON
for most things, but
USING
is a handy shorthand for the situation where
the column names are the same.
Consider this example dataset:
mysql> select * from pets; +---------+---------+--------+-----------+ | pets_id | animal | name | owners_id | +---------+---------+--------+-----------+ | 1 | fox | Rusty | 2 | | 2 | cat | Fluffy | 2 | | 3 | cat | Smudge | 3 | | 4 | cat | Toffee | 3 | | 5 | dog | Pig | 3 | | 6 | hamster | Henry | 1 | | 7 | dog | Honey | 1 | …[Read more]