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.

PHP Forum


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



Freelance Jobs

Reply
Is there a better way?
Old 02-03-2010, 12:55 PM Is there a better way?
Junior Talker

Posts: 3
Name: Ulrik Hvide
Location: Denmark
Trades: 0
Hey guys, I'm just working on a small php project (blog) and was wondering whether a basic select command as the one posted below could be optimized in any way (measured in loading time and CPU usage). Feel free to correct wrong syntax or whatever u find.

Title is title obviously, prev is preview which is a short description. Sorry for using small variable names, but it's a bad habit that doesn't bother me in these small scripts. Anyway, have fun and thank you for helping / debating.

PHP Code:
<div id="b">
// 10-15 lines of html here
<ul>
<?php 
flush
();
$a=mysql_connect('localhost','user','pass');
mysql_select_db('database',$a) or die();
$b=mysql_query('SELECT id,title,prev FROM b1 ORDER BY id limit 5');
mysql_close($a);
while(
$c=mysql_fetch_array($b)){
echo 
'<li><h2><a href="#">'.$c['title'].'</a></h2>'.$c['prev'].'</li>';
}
?>
</ul>

Last edited by Hv1de; 02-03-2010 at 01:33 PM..
Hv1de is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 02-03-2010, 01:13 PM Re: Is there a better way?
lynxus's Avatar
Awesomeo-Maximo

Posts: 1,618
Location: UK
Trades: 1
Not that i can see.

Its pretty specific already.
__________________

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-03-2010, 10:05 PM Re: Is there a better way?
vivekar's Avatar
Webmaster Talker

Posts: 612
Trades: 0
You can try stored procedures. Compiled statements will help improve the performance, but you need to have more data to see the difference.
__________________

Please login or register to view this content. Registration is FREE
(Active since 2003) |
Please login or register to view this content. Registration is FREE
vivekar is offline
Reply With Quote
View Public Profile Visit vivekar's homepage!
 
Old 02-04-2010, 04:06 AM Re: Is there a better way?
mtishetsky's Avatar
King Spam Talker

Posts: 1,226
Name: Mike
Location: Mataro, Spain
Trades: 0
If "id" is a primary key here, the query of such kind cannot be any faster than now. Though you may tune mysql cache if this query is requested significantly more often than new row is inserted, to make your mysql return the cached result instead of fetching the data each time. In this case you should keep in mind that mysql cache of all queries for the given table is reset every time a change is made to a table. Any change, not only insert.
__________________

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

And don't forget to give me talkupation!
mtishetsky is offline
Reply With Quote
View Public Profile Visit mtishetsky's homepage!
 
Old 02-04-2010, 04:34 AM Re: Is there a better way?
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
As already pointed out, you won't see any differance unless you handle large amount of data. Since you're already using a limit of 5 rows, no matter what changes you were to make the time for just running the query will just be 0.00 seconds.
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 02-04-2010, 09:46 AM Re: Is there a better way?
Junior Talker

Posts: 3
Name: Ulrik Hvide
Location: Denmark
Trades: 0
Thanks for the helpful replies, this is very theoretical question, but which of the following two examples would give the best performance? Lets assume that I changed the limit from 5 to 5000.

PHP Code:
<a href="'.$c['title'].'</a></h2>'.$c['prev'].'</li>'; 
Where the title includes both the url and the title, or:
PHP Code:
<a href="'.$c['url'].'">'.$c['title'].'</a></h2>'.$c['prev'].'</li>'; 
Basically the question is whether to go for less sql lookups at the price of bigger sql cells? Another question is how PHP will react to the inappropriate " (quotation mark)? The script does work, but will php be slowed down by the quotation mark?

Thanks for the help guys

Last edited by Hv1de; 02-04-2010 at 09:48 AM..
Hv1de is offline
Reply With Quote
View Public Profile
 
Old 02-04-2010, 01:54 PM Re: Is there a better way?
mtishetsky's Avatar
King Spam Talker

Posts: 1,226
Name: Mike
Location: Mataro, Spain
Trades: 0
You definitely are optimizing wrong things in wrong place. The difference in these particular examples will be 0.0001%, it isn't even worth discussing.
__________________

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

And don't forget to give me talkupation!
mtishetsky is offline
Reply With Quote
View Public Profile Visit mtishetsky's homepage!
 
Old 02-04-2010, 02:08 PM Re: Is there a better way?
Junior Talker

Posts: 3
Name: Ulrik Hvide
Location: Denmark
Trades: 0
Quote:
Originally Posted by mtishetsky View Post
You definitely are optimizing wrong things in wrong place. The difference in these particular examples will be 0.0001%, it isn't even worth discussing.
First of all, my question was theoretical, secondarily your argument is invalid. This isn't about optimizing, it's about learning to code without wasting resources on things that could be solved in a more innovative way. I agree with you that this example is absolutely a waste of time, but you failed to see the big picture.

Could anyone answer my last question please?
Hv1de is offline
Reply With Quote
View Public Profile
 
Old 02-04-2010, 04:32 PM Re: Is there a better way?
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
No matter how theoretical, or how big you scale this problem, it won't make any difference. Weather you retrieve 2 cells with x data, or 1 cell with 2x data, the same amount of data must still be retrieved. Maybe it will differ on some bits, depending on the internal structure to store the data, but the size of your application to actually notice any differance must be ridiculous.

Just choose the more logical one, which makes your job easier. In this specific case it makes more sense to store title and url individually.
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Reply     « Reply to Is there a better way?
 

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.67586 seconds with 12 queries