Thank you for responding!
Originally posted by Weedpacket
1) Only select the fields you need instead of selecting all of them with * (for example, you don't need to select banners.active because you know it's going to be equal to '1').
Yes you right here! Thnx for noticing this...
2) Are you using the appropriate types for your fields?
3) Do you have some suitable indexes defined?
I have an agent that optimises tables once per day...
Actually table structure is this:
CREATE TABLE `banners` (
`id` int(11) NOT NULL auto_increment,
`uid` int(11) NOT NULL default '0',
`pid` int(11) NOT NULL default '0',
`aid` int(11) NOT NULL default '0',
`url` varchar(250) default NULL,
`title` varchar(50) default NULL,
`clics` int(11) NOT NULL default '0',
`displays` int(11) NOT NULL default '0',
`cpmdispl` int(11) NOT NULL default '0',
`cdate` datetime NOT NULL default '0000-00-00 00:00:00',
`type` tinyint(4) NOT NULL default '0',
`img_type` tinyint(2) NOT NULL default '0',
`behaviour` tinyint(2) NOT NULL default '0',
`location` varchar(255) default NULL,
`txt` text,
`height` mediumint(9) NOT NULL default '0',
`width` mediumint(9) NOT NULL default '0',
`active` tinyint(1) NOT NULL default '1',
`last_display` datetime NOT NULL default '0000-00-00 00:00:00',
PRIMARY KEY (`id`),
KEY `id` (`id`),
KEY `uid` (`uid`),
KEY `pid` (`pid`),
KEY `aid` (`aid`),
KEY `height` (`height`),
KEY `width` (`width`)
) TYPE=MyISAM;
4) Joining tables is not a bad thing: it's the whole point of relational databases. Are your tables properly normalised?
5) If you want to make it easier to read, consider writing it on more than one line.
I format my code to be readable, but I don't know how this wysiwyg parses the code when displaying it...
if( !isset( $bid ) or !$bid
or !isset( $uid ) or !$uid
or !isset( $aid ) or !$aid
or !isset( $pid ) or !$pid
or !($qid = db_query( "SELECT banners.*, programs.cpm as pcpm, affprog.cpm AS apcpm, programs.name AS pname, users.flags AS aff_flags ".
" FROM banners, programs, affprog, users ".
" WHERE users.id = '".$aid."' AND banners.id = '".$bid."' AND banners.uid = '".$uid."' AND banners.pid = '".$pid."' AND ".
" banners.active = '1' AND programs.id = '".$pid."' AND affprog.uid = '".$uid."' AND ".
" affprog.pid = '".$pid."' AND affprog.aid = '".$aid."' AND affprog.deleted = '0' AND ".
" affprog.active = '".$PROGRAM_ACCEPTED."' AND programs.active = '".$PROGRAM_ACCEPTED."' AND ".
" programs.state = '1'" ))
or !($banner = mysql_fetch_object( $qid )) )
{
// put a vid banner here
if( isset( $btype ) )
....
}
6) What does db_query() do? If it's part of an abstraction layer, why do you then use mysql_fetch_object() instead of the layer's function?
I don't use a class for mysql connection, as a simple function library is enough for me...
7) Why do you check the value of $banner->active? It's going to be '1', isn't it? That's one of the conditions you're selecting on.
That was an example thinking that removing some conditions from mysql query and adding them at php level would make mysql's life easier with running this query...
I saw a mrtg report on net running on a mysql server that had 60k queries/sec for few months. This php module is the most accessed module on the site bcuz each affiliate that subscribes a webmaster program will use a javascript script that will call banner.php module. So this query will be run many times on mysql.
Some guys said that joining that many tables in a script that is run very often is not a good thing for mysql server which may crash bcuz of that. And I was wondering if it's that bad I done it like this...
Thnx again for your patience...