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
…
Introduction For a long time MySQL server supported only one trigger for every action (INSERT, UPDATE, DELETE) and timing (BEFORE or AFTER). In other words, there could be at most one trigger for every pair (action, timing). It means that a user couldn’t assign for example two BEFORE INSERT triggers for the same table t1. To workaround this restriction and allow several actions to fire on some table event, a user had to implement several stored procedures (one for each activity that would be implemented as independent trigger), create trigger for a table and call this stored procedures from the trigger. As of MySQL 5.7.2 this limitation has been removed. It means that starting the MySQL 5.7.2 a user can create for example, two BEFORE INSERT triggers, three AFTER INSERT triggers and four BEFORE UPDATE triggers for table t1. And this triggers will be called in the prescribed order determined (in generally) by the sequence in which triggers were …
[Read more]Introduction For a long time there was a Bug#6295 in implementation of BEFORE triggers related to handling of NOT NULL column. The problem was that if a column is declared as NOT NULL, it wasn’t possible to do INSERT NULL (or UPDATE to NULL) even though there was associated trigger, setting NOT-NULL value.
For example:
- There is the table ‘t1′ with a NOT NULL column ‘c1′
- The table has BEFORE INSERT trigger which sets the ‘c1′ column to NOT NULL value (SET NEW.c1 = 1)
- User executes the SQL statement INSERT INTO t1 VALUES(NULL) that fails with the following error: ERROR 1048 (23000): Column ‘c1′ cannot be null
- The user will get the same error if there is a BEFORE UPDATE trigger that sets the ‘c1′ column to NOT NULL …
Replication as most people know it, has mostly been SQL statement propagation from master to slave. This is known as "statement-based" replication. But there is also another kind of replication that is available, "the row-based replication" and that has quite a lot of benefits. In this post I intend on highlighting the advantages and disadvantages of both the types of replication to help you choose the best one. I also follow up with my own recommendation.
By now you know that there is a MySQL Track during next week’s ODTUG Kaleidoscope in Washington, DC. Ronald Bradford and I organized the schedule at the last minute (Ronald did a lot of the work!). It was difficult to fill a schedule with 19 sessions that are either 1 hour or 1.5 hours long, and to do it I ended up with three presentations.
At each presentation I will be giving away a copy of The MySQL Administrator’s Bible, so be sure to show up! All MySQL track sessions are in Maryland C, and all times are Eastern.
On Monday, June 28th from 4 pm – 5:30 pm I will be presenting …
[Read more]Accessing metadata can be optimized by using stored routines. Stored routines provide the ability to filter the data in a more useful way. For example, when I'm looking at table data I usually want to look at the index information also. So I use a stored routine called tabinfo that gives me key information I need for tables and indexes.
-- Create the tabinfo stored procedure. DROP PROCEDURE
Ever wondered how fast stored routines are in MySQL? I just ran a quick micro-benchmark to compare the speed of a stored function against a "roughly equivalent" subquery. The idea -- and there may be shortcomings that are poisoning the results here, your comments welcome -- is to see how fast the SQL procedure code is at doing basically the same thing the subquery code does natively (so to speak).
Before we go further, I want to make sure you know that the queries I'm writing here are deliberately mis-optimized to force a bad execution plan. You should never use IN() subqueries the way I do, at least not in MySQL 5.1 and earlier.
I loaded the World sample database and cooked up this query:
PLAIN TEXT SQL:
- SELECT sql_no_cache sum(ci.Population) FROM City AS ci
- WHERE …