Kemayo ([info]kemayo) wrote,

MySQL Subqueries Considered Harmful

(The title is a geeky injoke. Sorry.)

While creating FicWad we've tried to minimize the number of database queries that occur on any given page. This is mostly a reaction to the almost-forgotten-now base code, which went a bit overboard on queries. The worst example was any page that lists stories; a full page of, say, 15 stories would perform 150 database queries. With a bit of work we made it do the same thing in, I think, three queries.

Sometimes this bites us, though, when we get overzealous. Specifically, when [info]suntyger wrote the forum she used a subquery to try to reduce the number of queries on the page. It turns out that MySQL has really badly optimised subqueries at this point in time. I just tried bundling out the subquery into a separate query, and the page is generated around 10 times faster.

So a warning! Avoid subqueries in MySQL if you can.

(Regular people who don't know SQL can find a quick tutorial here.)
Tags: ficwad, spouse

  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    Your reply will be screened

    Your IP address will be recorded 

  • 6 comments

[info]ghostfaceposta

August 4 2005, 06:31:29 UTC 6 years ago

So a warning! Avoid subqueries in MySQL if you can.

You could have left out the "subqueries in" :)

[info]kemayo

August 4 2005, 06:34:56 UTC 6 years ago

I grant you that it's not a "real" relational database yet, but it's good for its target market. :-)

(Also, good luck avoiding MySQL in the low-end hosting market...)

[info]ghostfaceposta

August 4 2005, 06:44:58 UTC 6 years ago

MySQL is pretty awesome! I just couldn't resist... XD

[info]kemayo

August 4 2005, 07:17:00 UTC 6 years ago

Ah, you're a bad man. I can tell from the picture. That's not the laviscious smirk of a gentleman. ;_;

[info]olorelei

August 5 2005, 01:53:15 UTC 6 years ago

Hey, thanks for the link to the tutorial. I'm sitting here with a freaking limo of a free OS, with the means to learn just about anything I could want to know about coding, and I wanted to learn how to create mysql databases but didn't know where to look for a simple introduction to the syntax. Everything I found was either too slowly paced to keep me interested or too far along to speak to how little I already knew, so I kept wandering off to learn something else first.

No kidding. Many smooches to you.

Oh, and please pass along a few of those smooches to my daughter. I was just wading through some old files today and found some e-mail that she and I had exchanged a little over five years ago, when she'd been living in Michigan, fighting off depression, thinking of studying drama formally some more. She'd given me some good belly laughs then, and I'd needed them. (I seem to remember seeing her call herself an "attention whore" recently, and it struck me as a Good Thing. Heh. I still think she and I are going to get together and write and perform a kick-butt show one day. . . probably something to do with how sick the institution of family is. . . not that I'm disappointed to have a son-in-law, mind you. :o)

[info]kemayo

August 18 2005, 08:17:42 UTC 6 years ago

If you want some more random sql tutorials, I have a few tagged with 'db' on del.icio.us. ^_^

(Although the tagging scheme has grown over time, and I've not gone back to add relevant tags to older entries... so 'db + tutorial' is worthless right now.)
Create an Account
Forgot your login or password?
Facebook Twitter More login options
English • Español • Deutsch • Русский…