subqueries do not get cached

WTF???

…the cache is not used for queries of the following types:

* Prepared statements
* Queries that are a subquery of an outer query
* Queries executed within the body of a stored function or trigger

http://dev.mysql.com/doc/refman/5.0/en/query-cache-operation.html

+

For certain cases, a correlated subquery is optimized. For example:

val IN (SELECT key_val FROM tbl_name WHERE correlated_condition)

Otherwise, they are inefficient and likely to be slow. Rewriting the query as a join might improve performance.

http://dev.mysql.com/doc/refman/5.1/en/correlated-subqueries.html

I have 5 nested views… from 1 to 4, I have GROUP BY, ORDER BY, JOIN of the data over themselfs and other amenities… and on a ~2milion rows table, it doesn’t take more than 14 seconds…

the last view… has a subquery… evaluating the data related to the “currently evaluated row”, looking on the same table for values of “the same group”… and it gets DAMN SLOW… cause the “query cache” mechanism of MySQL does not kick in

k, tomorrow morning I go for the rewriting… I’ll create a VIEW about the data I need to be correlated, and I’ll JOIN it… let’s see 🙂

Advertisement

2 thoughts on “subqueries do not get cached

  1. Provalo su Oracle, se non hai problemi di spazio va benissimo anche la XE, e la tua query verrà eseguita in frazioni di secondo.

    L’SQL vorrebbe essere una linguaggio prettamente dichiarativo: tu devi solo dichiarare cosa vuoi, il come ottenerlo nel modo più rapido è compito dell’ottimizzatore del DBMS.

    La vera differenza tra un DBMS con i fiocchi, Oracle ad esempio, ed un giocattolo, MySQL ad esempio, è che con il primo scrivi SQL senza patemi d’animo e funziona sempre in modo estremamente efficiente, con il secondo devi stare molto molto attento a come scrivi o riscrivi le query.
    Inutilmente frustrante!

  2. @Ricky: riscrivendo la subquery come VIEW a latere e facendone una join con la struttura dati che mi serviva, ho ridotto i tempi di elaborazione da “non gira rimanendo appesa per ore” a 6 minuti e 30secondi… e credo ci sia ancora spazio per ottimizzazioni. Oracle sara’ anche fico, ma ho appena risparmiato $badalucco$ di soldi! 🙂

    Per ulteriori ottimizzazioni estreme… sarei potuto andare su questa branch di sviluppo… ma sto lasciando l’azienda e non mi va di lasciare merdoni in giro che altri non saprebbero gestire…

    http://www.facebook.com/MySQLatFacebook
    https://launchpad.net/mysqlatfacebook

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s