View Full Version : PHP vs mySQL
NancyJ
08-11-2006, 01:08 PM
Which is faster PHP or mySQL?
Now before you get all knotted ;) hear me out.
If you can do something in PHP that you can do in mySQL - should you? Which is faster, which uses more resources?
For example, is it faster or more efficient to select count() or to select and then count the result with a loop in PHP?
How about max or distinct?
Just because you can, does that mean you should?
Phill
08-11-2006, 01:11 PM
For example, is it faster or more efficient to select count() or to select and then count the result with a loop in PHP?
Database. Databases have indexes on those things, there are far more efficient ways to count the number of rows in a database table then iterate all the way through in a for loop (or similar).
Also, all the data doesn't have to go through IPC (inter process communication i.e. via a socket), it can just be done in one process, which I believe speeds up the process.
How about max or distinct?
Ditto.
In general, I'd say data filtering should be done at the database level. That's what they're designed for :D
guelphdad
08-11-2006, 01:35 PM
I'd agree. Do your work in your database, display your results with your application language.
NancyJ
08-11-2006, 01:56 PM
I do use the db for everything possible but it seems to use a lot of CPU when I do(yes they're all indexed) - I've never actually tested to time the result - wondered if anyone had.
cubsfan
08-11-2006, 09:41 PM
I would test your queries using explain and make sure they are using the appropriate index.
http://mysql.com/doc/refman/5.0/en/explain.html
I've had instances I've had to use a force index to make it use the right one.
YMMV, can't really speak to the original question, haven't tested :)
gsnedders
08-11-2006, 10:11 PM
Any interpreted language (eg. PHP) will normally be slower than a compiled language (eg. C++, which MySQL is coded in).
marek_mar
08-12-2006, 04:44 AM
This is actually interesting. PHP is the name of the language aswell as the interpretor of the language. The PHP intrepretor is written in c++ just like MySQL. MySQL interprest SQL which is not compiled just like PHP code.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.