Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

The Database Forum


You are currently viewing our The Database Forum as a guest. Please register to participate.
Login



Reply
Why does a query on the mysql server itself run quicker than in php..
Old 02-05-2011, 06:52 PM Why does a query on the mysql server itself run quicker than in php..
lynxus's Avatar
Awesomeo-Maximo

Posts: 1,618
Location: UK
Trades: 1
Hi Guys,

Just wondering..

Why is it that when you ( mysql -uUSERNAME -pPASSWORD )
and then run a query.
Its like UBER quick and uses hardly any CPU.. ( 0.00 sec )

However the same query thats run through PHP takes like 1second and uses 99% cpu when it happens?

Ive just noticed this and wonders if these some odd setting in php misconfigured?
__________________

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE


lynxus is offline
Reply With Quote
View Public Profile Visit lynxus's homepage!
 
 
Register now for full access!
Old 02-05-2011, 08:26 PM Re: Why does a query on the mysql server itself run quicker than in php..
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,385
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Perfectly usual.

Running the query directly from a command line will always be quicker than calling the same query coded in PHP or in phpmyadmin as there is no overhead.

PHP has to parse the input string, pass it to the SQL server, wait for the response, return to the php process and format the data for the output.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 02-06-2011, 05:42 AM Re: Why does a query on the mysql server itself run quicker than in php..
lynxus's Avatar
Awesomeo-Maximo

Posts: 1,618
Location: UK
Trades: 1
Quote:
Originally Posted by chrishirst View Post
Perfectly usual.

Running the query directly from a command line will always be quicker than calling the same query coded in PHP or in phpmyadmin as there is no overhead.

PHP has to parse the input string, pass it to the SQL server, wait for the response, return to the php process and format the data for the output.
Dont suppose you know if any techniques to speed this up?
While its not an issue yet, It would be good to know for later.
__________________

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE


lynxus is offline
Reply With Quote
View Public Profile Visit lynxus's homepage!
 
Old 02-06-2011, 05:57 AM Re: Why does a query on the mysql server itself run quicker than in php..
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,385
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
It all depends on the type of queries you are running.

Query caching can help.
Using stored procedures.
Optimising queries by using a field list rather than "SELECT * FROM".
Filtering and/or sorting in the database rather than in server side code.

To be honest implementing code and queries that are optimised for speed is much easier from day one than it is to "reftro fit" when and if it becomes a neccessity.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 02-06-2011, 06:11 AM Re: Why does a query on the mysql server itself run quicker than in php..
lynxus's Avatar
Awesomeo-Maximo

Posts: 1,618
Location: UK
Trades: 1
This is something i did try But only recently found some bad queries

For example: ( using * )
SELECT DISTINCT * from visitorips where siteid = '$siteid' order by id desc limit 30
Takes about 1second to execute. ( Edit: Sorry i mean 5secs.. )

HOWEVER!
If i change the where from = to "like"
SELECT DISTINCT * from visitorips where siteid LIKE '$siteid' order by id desc limit 30
It states it takes 0.00 seconds and works fine.
( I then have some PHP code that checks the siteids "really" match after.

This has increased performance by like 700%

I suppose i should change it from * to the fields..
__________________

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE



Last edited by lynxus; 02-06-2011 at 06:20 AM..
lynxus is offline
Reply With Quote
View Public Profile Visit lynxus's homepage!
 
Old 02-06-2011, 06:18 AM Re: Why does a query on the mysql server itself run quicker than in php..
lynxus's Avatar
Awesomeo-Maximo

Posts: 1,618
Location: UK
Trades: 1
Ive optimized the hell outta every other query i could find.
I cant stand seeing something take longer than 0.00 sec.
lol
__________________

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE


lynxus is offline
Reply With Quote
View Public Profile Visit lynxus's homepage!
 
Old 02-06-2011, 06:44 AM Re: Why does a query on the mysql server itself run quicker than in php..
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,385
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
There is no "right way" to optimise queries as it depends to some extent on the type of data and the table structures.

And of course with MySQL the storage engine can introduce bottlenecks.

For example; MyISAM uses "table locking" during updates which will delay other queries, whereas InnoDb uses record (row) locking. So if your script/application does a lot of writing to the db InnoDb should be your engine of choice.

Testing the various options with real data is the way to learn what works best for various scenarios/schemas.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 02-06-2011, 09:18 AM Re: Why does a query on the mysql server itself run quicker than in php..
lynxus's Avatar
Awesomeo-Maximo

Posts: 1,618
Location: UK
Trades: 1
Cool, Thanks for the insight.
Im not DBA so still learning.. Then again that's inherent when working in I.T...

Id say I do about 98% Selecting and 2% ( maybe less ) doing updates and deletes.
My engine is MyISAM so should do for the moment.

Thanks again man.

Ive also more than halfed my server load from sorting my queries out. ( System gets about 50k hits a day )

So rather than clustering the frontend further .. This has helped a lot.
__________________

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE



Last edited by lynxus; 02-06-2011 at 09:19 AM..
lynxus is offline
Reply With Quote
View Public Profile Visit lynxus's homepage!
 
Old 02-07-2011, 01:59 PM Re: Why does a query on the mysql server itself run quicker than in php..
Average Talker

Posts: 23
Trades: 0
Quote:
Originally Posted by chrishirst View Post
Perfectly usual.

Running the query directly from a command line will always be quicker than calling the same query coded in PHP or in phpmyadmin as there is no overhead.

PHP has to parse the input string, pass it to the SQL server, wait for the response, return to the php process and format the data for the output.
You missed an overhead with PHP i.e. connection to database.
__________________

Please login or register to view this content. Registration is FREE

Last edited by mwasif; 02-07-2011 at 02:01 PM..
mwasif is offline
Reply With Quote
View Public Profile Visit mwasif's homepage!
 
Reply     « Reply to Why does a query on the mysql server itself run quicker than in php..
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.55891 seconds with 12 queries