Forum Moderators: open

Message Too Old, No Replies

MySQL vs. PostgreSQL stored procedures

Which is faster/more mature?

         

jtara

9:39 pm on Mar 10, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I just finished a project for a client using in part MySQL. The project consisted of bug fixes, new features, and speedup of an existing application written in Python. The software originally used SQLite.

I managed to speed up the most important query by 2000%, and eliminated locking problems (SQLite locks the ENTIRE database against write during a read) which resulted in loss of data when the database was locked for too long. (Hardware devices sending updates, and the server code gives up after 15 seconds.)

To get that kind of improvement, I fiddled with the queries in every which way. One of my more recent brilliant ideas was that - OBVIOUSLY - using stored procedures would be faster.

Right? Stored procedures are obviously faster. Everybody thinks that.

Wrong.

Not in MySQL.

The code had a sub-query that was "hand coded". That is, it did a query on a "parent" table, then looped through the results, doing a query on a "child" table for each.

Now, obviously, that would be faster if I did it all in one query, right?

Not in MySQL.

Maybe I'm just rusty at SQL-slinging. But the best I could do with a single SQL statement with the subquery was 2 seconds for a certain query. With the query-in-a-loop, it's .5 seconds. (The original code with SQLite took 10 seconds - that's with the looped query. No, it wasn't just SQLite vs. MySQL - SQLite is actually pretty darn close to MySQL speed-wise in many ways. It was just plain awful code.)

So, once I figured gave up at coming up with a single statement that was faster than using the loop, I figured... hey, why repeated send that query to the server - put it in a stored procedure!

I actually found an acknowledgment on the MySQL website that stored procedures - at least at present - are significant slower than inline ones. They try to make the case that if you are using stored procedures, you should be doing it to encapsulate business logic - not with an expectation of speeding things up.

Turns out that MySQL doesn't take advantage of query caching when stored procedures are used.

Oh, yea, I can hear you thinking.... prepared statements. And, I'll bet you can already hear my answser:

Not in MySQL. (not faster, that is - they are slower).

Now, I need to make a decision on which database to use on one of my OWN projects. I'd really like to use stored procedures.

Anybody used them in both MySQL and PostGreSQL? I know Postgres has had them longer. Are they also faster?

Or am I fooling myself, and is this still the domain of Oracle alone?

Some rough numbers:

Original/SQLite 10 seconds

MySQL immediately after naive rewrite (just resolve some MySQL syntax differences) - 4 seconds.

MySQL tweaked, still using loop: 0.5 seconds.

MySQL single statement - 2 seconds.

MySQL tweaked, using loop, using a stored procedure: 1 second

For reference, here's a sanitized version of the single statement I cam up with. (Note that I'm omitting a whole buncha WHERE.)


/* Generic design pattern
*
* You can arbitrarily extend this to "n" widget events */
SELECT
widget.*, latest_event.*, previous_event.*
FROM
widget

LEFT OUTER JOIN
widget_event AS latest_event
ON
(
widget.wid = latest_event.wid AND latest_event.timestamp =
(
SELECT
timestamp
FROM
widget_event
WHERE
wid = widget.wid
ORDER BY timestamp DESC
LIMIT 1
)
)

LEFT OUTER JOIN
widget_event AS previous_event
ON
(
widget.wid = previous_event.wid AND previous_event.timestamp =
(
SELECT
timestamp
FROM
widget_event
WHERE
wid = widget.wid AND timestamp < latest_event.timestamp
ORDER BY timestamp DESC
LIMIT 1
)
)
ORDER BY widget.id ASC;

coopster

10:08 pm on Mar 13, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



(SQLite locks the ENTIRE database against write during a read) which resulted in loss of data when the database was locked for too long. (Hardware devices sending updates, and the server code gives up after 15 seconds.)

It's an embedded database as opposed to client/server. Sounds like it was being used beyond intended design? [sqlite.org...]

I ran some tests against a MySQL database using subselects versus JOIN queries and found time and again the JOIN performed much faster. UNION takes the cake when it comes to speed. I am yet to be impressed with subselects though. I haven't pitched MySQL against PostGreSQL in a real time test yet though, so cannot offer experience. Saving that project for a rainy day ...

If you haven't already, use EXPLAIN on your MySQL query to see where you might improve the query performance. Indexes often put them into the subsecond response category.

Another impressive database is IBM DB2. The midrange systems (iSeries, previously known as AS400) have a client utility called "Query Analyzer" or something along those lines that is quite impressive for optimizing queries.

coopster

10:10 pm on Mar 13, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



And I realized that I just babbled openly and really didn't come anywhere close to offering anything in regards to your original question ...

"Which is faster/more mature in regards to stored procedures, MySQL or PostGreSQL?"

... except for, "I have no idea at this time." Begging your forgiveness here ...

jtara

5:37 pm on Mar 14, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's an embedded database as opposed to client/server. Sounds like it was being used beyond intended design?

The client makes little black boxes - literally - that run uCLinux.

The product I worked on is a tool for managing their little black boxes. I'm guessing that they just used the tools they were familiar with - they are little black box guys, not server guys...

I think I'm going to give PostGres (8.3) a go and do some benchmarking against MySQL. I like the fact that I can use Python or Ruby to write stored procedures.

I will be using either Rails or Pylons and want to use an ORM for rapid development, but I think that ORMs are too immature yet for very complex relationships. So I'd like to use stored procedures, views, etc. to provide a pre-crunched, simplified view of the database to the ORM.

Demaestro

6:41 pm on Mar 14, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



jtara,

I have been using Postegres for about 7 years now.

Postgres has rules, triggers, server-side functions that can be written in C, pgsql, python, perl and tcl languages. In my experience even poorly written pl/pgsql code runs really fast.

What I know about MySql is rudimentary support for triggersdidn't start until MySQL 5.0.2. so they are quite "young" still

I haven't used them at all in yet though but I was reading that an external development implemented in perl can be used as stored procedures in Mysql. Again I have no XP with doing that, I just remember reading about someone having success with it.

There is also that thing about 2 filesort algos. I wonder which is used by default when using Stored Procedures in MySql and if it can use your indexes for the "order by" statement.

[dev.mysql.com...]

I am using Postgres 8.1.11 everywhere so if you have any questions let me know.

To answer the question though I would say Postgres is faster.

[edited by: Demaestro at 6:43 pm (utc) on Mar. 14, 2008]