php - Optimizing sql statements to reduce MySQL server load -


$sql1 = "select questions last_check_date user_id=? order questions desc limit 1"; $sql2 = "select id questions add_dt>?"; 

what statements above do?

when execute sql1, gets last check date user.

then i'm executing second query, fetch id's add date>last check date (from sql1) , return affected rows count.

what want merge 2 statements 1, , optimize query count. following problem may occur:

there no row user in $sql1: must select rows in sql2 , return affected rows count.

i can't figure out how must like.. thx in advance

update

show create table last_check_date; result is

create table `last_check_date` (   `id` int(11) unsigned not null,   `user_id` bigint(20) unsigned not null,   `questions` datetime default null,   `users` datetime default null,   primary key (`id`) ) engine=innodb default charset=utf8 

and show create table questions;

create table `questions` (   `id` int(11) unsigned not null auto_increment,   `author_id` bigint(20) unsigned default null,   `question` text not null,   `var_a` text not null,   `var_b` text not null,   `var_c` text not null,   `var_d` text not null,   `var_e` text not null,   `subject` int(11) unsigned default null,   `chapter` int(11) unsigned default null,   `section` int(11) unsigned default null,   `paragraph` int(11) unsigned default null,   `rank` tinyint(2) not null,   `add_dt` datetime not null,   `answer` varchar(1) not null,   primary key (`id`),   key `fk_chapters-id` (`chapter`),   key `fk_paragraphs-id` (`paragraph`),   key `fk_subjects-id` (`subject`),   key `fk_sections-id` (`section`),   key `fk_author-id` (`author_id`),   constraint `fk_author-id` foreign key (`author_id`) references `users` (`id`) on delete set null on update cascade,   constraint `fk_chapters-id` foreign key (`chapter`) references `chapters` (`id`) on delete set null on update cascade,   constraint `fk_paragraphs-id` foreign key (`paragraph`) references `paragraphs` (`id`) on delete set null on update cascade,   constraint `fk_sections-id` foreign key (`section`) references `sections` (`id`) on delete set null on update cascade,   constraint `fk_subjects-id` foreign key (`subject`) references `subjects` (`id`) on delete set null on update cascade ) engine=innodb auto_increment=4 default charset=utf8 

$sql = "     select q.id      questions q     left join (         select questions          last_check_date          user_id=?          order questions          desc limit 1     ) l on q.add_dt > l.questions"  $rs = mysql_query($sql); $rowcount = mysql_num_rows($rs); 

i don't know yet proper syntax pdo/mysqli, please adapt prefered driver.


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -