By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,718 Members | 1,840 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,718 IT Pros & Developers. It's quick & easy.

Problem with executing MySQL queries through PHP.

P: n/a
Daz
Hi everyone.

I was faced with the choice of whether my problem is indeed a PHP
problem or a MySQL. I have decided it's a PHP problem as I don't
experience the same problem when I execute the same query at the CLI.

I am having trouble executing a large query through my PHP script. It
takes about 7-11 seconds on
average to execute, whereas the same query only takes 0.01 seconds to
execute through the CLI.

I thought that it could have been something to do with the table
collation, and I have changed that to alsorts of different things,
still to no avail. For some reason the problem appeared to have occured
from out of the blue. It all worked fine, and then all of a sudden
started going slow. The rest of the queries executed on the database
are very fast, it only seems to be when I use the tables that I have
recently created for my project that they run slow.

I am joining three tables together:

================================================== ==========

CREATE TABLE IF NOT EXISTS `cms_users` (
`user_id` mediumint(8) unsigned NOT NULL auto_increment,
`name` varchar(60) default NULL,
`username` varchar(50) NOT NULL default '',
`user_email` varchar(255) NOT NULL default '',
`femail` varchar(255) default NULL,
`user_website` varchar(255) default NULL,
`user_avatar` varchar(255) NOT NULL default '',
`user_regdate` varchar(20) NOT NULL default '',
`user_icq` varchar(15) default NULL,
`user_occ` varchar(100) default NULL,
`user_from` varchar(100) default NULL,
`user_interests` varchar(150) default NULL,
`user_sig` varchar(255) default NULL,
`user_viewemail` tinyint(4) NOT NULL default '0',
`user_aim` varchar(35) default NULL,
`user_yim` varchar(40) default NULL,
`user_msnm` varchar(40) default NULL,
`user_password` varchar(40) NOT NULL default '',
`storynum` tinyint(4) NOT NULL default '10',
`umode` varchar(10) default NULL,
`uorder` tinyint(4) NOT NULL default '0',
`thold` tinyint(4) NOT NULL default '0',
`noscore` tinyint(4) NOT NULL default '0',
`bio` tinytext,
`ublockon` tinyint(4) NOT NULL default '0',
`ublock` tinytext,
`theme` varchar(255) NOT NULL default '',
`commentmax` int(11) NOT NULL default '4096',
`counter` int(11) NOT NULL default '0',
`newsletter` tinyint(4) NOT NULL default '0',
`user_posts` int(11) NOT NULL default '0',
`user_attachsig` tinyint(4) NOT NULL default '1',
`user_rank` int(11) NOT NULL default '0',
`user_level` tinyint(4) NOT NULL default '1',
`user_active` tinyint(4) default '1',
`user_session_time` int(11) NOT NULL default '0',
`user_lastvisit` int(11) NOT NULL default '0',
`user_timezone` varchar(6) NOT NULL default '0',
`user_dst` smallint(6) NOT NULL default '0',
`user_style` tinyint(4) default NULL,
`user_lang` varchar(255) NOT NULL default 'english',
`user_dateformat` varchar(14) NOT NULL default 'D M d, Y g:i a',
`user_new_privmsg` smallint(5) unsigned NOT NULL default '0',
`user_unread_privmsg` smallint(5) unsigned NOT NULL default '0',
`user_last_privmsg` int(11) NOT NULL default '0',
`user_emailtime` int(11) default NULL,
`user_allowhtml` tinyint(4) default '1',
`user_allowbbcode` tinyint(4) default '1',
`user_allowsmile` tinyint(4) default '1',
`user_allowavatar` tinyint(4) NOT NULL default '1',
`user_allow_pm` tinyint(4) NOT NULL default '1',
`user_allow_viewonline` tinyint(4) NOT NULL default '1',
`user_notify` tinyint(4) NOT NULL default '0',
`user_notify_pm` tinyint(4) NOT NULL default '0',
`user_popup_pm` tinyint(4) NOT NULL default '0',
`user_avatar_type` tinyint(4) NOT NULL default '3',
`user_sig_bbcode_uid` varchar(10) default NULL,
`user_actkey` varchar(32) default NULL,
`user_newpasswd` varchar(32) default NULL,
`user_group_cp` int(11) NOT NULL default '2',
`user_group_list_cp` varchar(100) NOT NULL default '2',
`user_active_cp` tinyint(4) NOT NULL default '1',
`susdel_reason` text,
PRIMARY KEY (`user_id`),
KEY `uname` (`username`),
KEY `user_session_time` (`user_session_time`)
) ENGINE=MyISAM DEFAULT charset=utf8 AUTO_INCREMENT=308 ;

DROP TABLE IF EXISTS `pp_books`;
CREATE TABLE IF NOT EXISTS `pp_books` (
`book_id` smallint(4) unsigned NOT NULL auto_increment,
`book_name` varchar(50) NOT NULL default '',
`is_retired` tinyint(1) unsigned default NULL,
PRIMARY KEY (`book_id`),
UNIQUE KEY `book_name` (`book_name`)
) ENGINE=MyISAM AUTO_INCREMENT=3670 ;

DROP TABLE IF EXISTS `pp_ubooks`;
CREATE TABLE IF NOT EXISTS `pp_ubooks` (
`uid` int(10) unsigned NOT NULL default '0',
`book_id` int(10) unsigned NOT NULL default '0'
) ENGINE=MyISAM DEFAULT charset=utf8;
================================================== ==========

(The last table has a unique index which spans across both columns).

This is the query I am executing:

SELECT
t1.letter AS letter,
COUNT(*) AS total_books_in_section,
SUM(IF(t1.retired='1',1,0 )) AS total_retired,
SUM(IF(t1.retired!='1',1,0 )) AS owned_regular,
SUM(IF(t1.retired='1' AND t2.bid IS NOT NULL,1,0 )) AS
owned_retired_books,
SUM(IF(t1.retired='1' AND t2.bid IS NULL,1,0)) AS
unowned_retired_books,
SUM(IF(t1.retired!='1' AND t2.bid IS NOT NULL,1,0)) AS
owned_regular_books,
SUM(IF(t1.retired!='1' AND t2.bid IS NULL,1,0)) AS
unowned_regular_books
FROM (
SELECT
book_id AS bid,
LEFT(book_name,1) AS letter,
IF(is_retired='1',1,0) AS retired
FROM
pp_books
) AS t1
LEFT JOIN (
SELECT
book_id AS bid,
cms_users.username AS username,
IF(book_id IS NULL,0,1) AS is_owned
FROM
pp_ubooks
RIGHT JOIN
cms_users
ON
cms_users.user_id=pp_ubooks.uid
WHERE
cms_users.username='$username'
OR
cms_users.username IS NULL
) AS t2
ON
t1.bid=t2.bid
GROUP BY
letter;

I am trying to comprehend what might have gone wrong to be causing this
problem. As I mentioned before, it takes only 0.01 seconds to execute
in both phpmyadmin and at the CLI. All other queries on my website
execute super-fast, just not any that use pp_ubooks or pp_books. Even
this query takes more than 5 seconds to execute on the website, but
only 0.00 seconds using the CLI.

Just for the record, my website and the CLI are using the same
database. I think it might have something to do with my table
collations, but I can't be sure if this is the case, and if it is, why
it has such a massive impact. I have tried setting my book tables to
latin-swedish-ci (which is the default), utf8-unicode-ci, and
utf8-general-ci (which all of my other tables are set to), and nothing
seems to work any better.

I have been utterly baffled by this mystery for days now, and if
haven't got any hair left on my head to pull out anymore. If anyone
could suggest anything they believe to be helpful I would really
appreciate it! I have even gone as far as to completely scrap apache2,
mysql-server-5.0 and php4 and to start the installation all over again.
This is what leads me to beleive it could be something to do with my
tables rather than the configuration of anything in particular, but I
am just guessing.

Best wishes.

Daz.

Oct 28 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Daz

Daz wrote:
Hi everyone.

I was faced with the choice of whether my problem is indeed a PHP
problem or a MySQL. I have decided it's a PHP problem as I don't
experience the same problem when I execute the same query at the CLI.

I am having trouble executing a large query through my PHP script. It
takes about 7-11 seconds on
average to execute, whereas the same query only takes 0.01 seconds to
execute through the CLI.

I thought that it could have been something to do with the table
collation, and I have changed that to alsorts of different things,
still to no avail. For some reason the problem appeared to have occured
from out of the blue. It all worked fine, and then all of a sudden
started going slow. The rest of the queries executed on the database
are very fast, it only seems to be when I use the tables that I have
recently created for my project that they run slow.

I am joining three tables together:

================================================== ==========

CREATE TABLE IF NOT EXISTS `cms_users` (
`user_id` mediumint(8) unsigned NOT NULL auto_increment,
`name` varchar(60) default NULL,
`username` varchar(50) NOT NULL default '',
`user_email` varchar(255) NOT NULL default '',
`femail` varchar(255) default NULL,
`user_website` varchar(255) default NULL,
`user_avatar` varchar(255) NOT NULL default '',
`user_regdate` varchar(20) NOT NULL default '',
`user_icq` varchar(15) default NULL,
`user_occ` varchar(100) default NULL,
`user_from` varchar(100) default NULL,
`user_interests` varchar(150) default NULL,
`user_sig` varchar(255) default NULL,
`user_viewemail` tinyint(4) NOT NULL default '0',
`user_aim` varchar(35) default NULL,
`user_yim` varchar(40) default NULL,
`user_msnm` varchar(40) default NULL,
`user_password` varchar(40) NOT NULL default '',
`storynum` tinyint(4) NOT NULL default '10',
`umode` varchar(10) default NULL,
`uorder` tinyint(4) NOT NULL default '0',
`thold` tinyint(4) NOT NULL default '0',
`noscore` tinyint(4) NOT NULL default '0',
`bio` tinytext,
`ublockon` tinyint(4) NOT NULL default '0',
`ublock` tinytext,
`theme` varchar(255) NOT NULL default '',
`commentmax` int(11) NOT NULL default '4096',
`counter` int(11) NOT NULL default '0',
`newsletter` tinyint(4) NOT NULL default '0',
`user_posts` int(11) NOT NULL default '0',
`user_attachsig` tinyint(4) NOT NULL default '1',
`user_rank` int(11) NOT NULL default '0',
`user_level` tinyint(4) NOT NULL default '1',
`user_active` tinyint(4) default '1',
`user_session_time` int(11) NOT NULL default '0',
`user_lastvisit` int(11) NOT NULL default '0',
`user_timezone` varchar(6) NOT NULL default '0',
`user_dst` smallint(6) NOT NULL default '0',
`user_style` tinyint(4) default NULL,
`user_lang` varchar(255) NOT NULL default 'english',
`user_dateformat` varchar(14) NOT NULL default 'D M d, Y g:i a',
`user_new_privmsg` smallint(5) unsigned NOT NULL default '0',
`user_unread_privmsg` smallint(5) unsigned NOT NULL default '0',
`user_last_privmsg` int(11) NOT NULL default '0',
`user_emailtime` int(11) default NULL,
`user_allowhtml` tinyint(4) default '1',
`user_allowbbcode` tinyint(4) default '1',
`user_allowsmile` tinyint(4) default '1',
`user_allowavatar` tinyint(4) NOT NULL default '1',
`user_allow_pm` tinyint(4) NOT NULL default '1',
`user_allow_viewonline` tinyint(4) NOT NULL default '1',
`user_notify` tinyint(4) NOT NULL default '0',
`user_notify_pm` tinyint(4) NOT NULL default '0',
`user_popup_pm` tinyint(4) NOT NULL default '0',
`user_avatar_type` tinyint(4) NOT NULL default '3',
`user_sig_bbcode_uid` varchar(10) default NULL,
`user_actkey` varchar(32) default NULL,
`user_newpasswd` varchar(32) default NULL,
`user_group_cp` int(11) NOT NULL default '2',
`user_group_list_cp` varchar(100) NOT NULL default '2',
`user_active_cp` tinyint(4) NOT NULL default '1',
`susdel_reason` text,
PRIMARY KEY (`user_id`),
KEY `uname` (`username`),
KEY `user_session_time` (`user_session_time`)
) ENGINE=MyISAM DEFAULT charset=utf8 AUTO_INCREMENT=308 ;

DROP TABLE IF EXISTS `pp_books`;
CREATE TABLE IF NOT EXISTS `pp_books` (
`book_id` smallint(4) unsigned NOT NULL auto_increment,
`book_name` varchar(50) NOT NULL default '',
`is_retired` tinyint(1) unsigned default NULL,
PRIMARY KEY (`book_id`),
UNIQUE KEY `book_name` (`book_name`)
) ENGINE=MyISAM AUTO_INCREMENT=3670 ;

DROP TABLE IF EXISTS `pp_ubooks`;
CREATE TABLE IF NOT EXISTS `pp_ubooks` (
`uid` int(10) unsigned NOT NULL default '0',
`book_id` int(10) unsigned NOT NULL default '0'
) ENGINE=MyISAM DEFAULT charset=utf8;
================================================== ==========

(The last table has a unique index which spans across both columns).

This is the query I am executing:

SELECT
t1.letter AS letter,
COUNT(*) AS total_books_in_section,
SUM(IF(t1.retired='1',1,0 )) AS total_retired,
SUM(IF(t1.retired!='1',1,0 )) AS owned_regular,
SUM(IF(t1.retired='1' AND t2.bid IS NOT NULL,1,0 )) AS
owned_retired_books,
SUM(IF(t1.retired='1' AND t2.bid IS NULL,1,0)) AS
unowned_retired_books,
SUM(IF(t1.retired!='1' AND t2.bid IS NOT NULL,1,0)) AS
owned_regular_books,
SUM(IF(t1.retired!='1' AND t2.bid IS NULL,1,0)) AS
unowned_regular_books
FROM (
SELECT
book_id AS bid,
LEFT(book_name,1) AS letter,
IF(is_retired='1',1,0) AS retired
FROM
pp_books
) AS t1
LEFT JOIN (
SELECT
book_id AS bid,
cms_users.username AS username,
IF(book_id IS NULL,0,1) AS is_owned
FROM
pp_ubooks
RIGHT JOIN
cms_users
ON
cms_users.user_id=pp_ubooks.uid
WHERE
cms_users.username='$username'
OR
cms_users.username IS NULL
) AS t2
ON
t1.bid=t2.bid
GROUP BY
letter;

I am trying to comprehend what might have gone wrong to be causing this
problem. As I mentioned before, it takes only 0.01 seconds to execute
in both phpmyadmin and at the CLI. All other queries on my website
execute super-fast, just not any that use pp_ubooks or pp_books. Even
this query takes more than 5 seconds to execute on the website, but
only 0.00 seconds using the CLI.

Just for the record, my website and the CLI are using the same
database. I think it might have something to do with my table
collations, but I can't be sure if this is the case, and if it is, why
it has such a massive impact. I have tried setting my book tables to
latin-swedish-ci (which is the default), utf8-unicode-ci, and
utf8-general-ci (which all of my other tables are set to), and nothing
seems to work any better.

I have been utterly baffled by this mystery for days now, and if
haven't got any hair left on my head to pull out anymore. If anyone
could suggest anything they believe to be helpful I would really
appreciate it! I have even gone as far as to completely scrap apache2,
mysql-server-5.0 and php4 and to start the installation all over again.
This is what leads me to beleive it could be something to do with my
tables rather than the configuration of anything in particular, but I
am just guessing.

Best wishes.

Daz.
Oh, and this is th query that takes more than 5 seconds when executed
through PHP:
SELECT book_id AS bid, LEFT(book_name,1) AS letter,
IF(is_retired='1',1,0) AS retired FROM pp_books;

Oct 28 '06 #2

P: n/a
How many results do you get back? If your recordset is huge, the render
time in your browser could be the problem rather than just the query.

What does php do to the records as it's outputting them? Any sort of
calculations?
Daz wrote:
Daz wrote:
Hi everyone.

I was faced with the choice of whether my problem is indeed a PHP
problem or a MySQL. I have decided it's a PHP problem as I don't
experience the same problem when I execute the same query at the CLI.

I am having trouble executing a large query through my PHP script. It
takes about 7-11 seconds on
average to execute, whereas the same query only takes 0.01 seconds to
execute through the CLI.

I thought that it could have been something to do with the table
collation, and I have changed that to alsorts of different things,
still to no avail. For some reason the problem appeared to have occured
from out of the blue. It all worked fine, and then all of a sudden
started going slow. The rest of the queries executed on the database
are very fast, it only seems to be when I use the tables that I have
recently created for my project that they run slow.

I am joining three tables together:

================================================== ==========

CREATE TABLE IF NOT EXISTS `cms_users` (
`user_id` mediumint(8) unsigned NOT NULL auto_increment,
`name` varchar(60) default NULL,
`username` varchar(50) NOT NULL default '',
`user_email` varchar(255) NOT NULL default '',
`femail` varchar(255) default NULL,
`user_website` varchar(255) default NULL,
`user_avatar` varchar(255) NOT NULL default '',
`user_regdate` varchar(20) NOT NULL default '',
`user_icq` varchar(15) default NULL,
`user_occ` varchar(100) default NULL,
`user_from` varchar(100) default NULL,
`user_interests` varchar(150) default NULL,
`user_sig` varchar(255) default NULL,
`user_viewemail` tinyint(4) NOT NULL default '0',
`user_aim` varchar(35) default NULL,
`user_yim` varchar(40) default NULL,
`user_msnm` varchar(40) default NULL,
`user_password` varchar(40) NOT NULL default '',
`storynum` tinyint(4) NOT NULL default '10',
`umode` varchar(10) default NULL,
`uorder` tinyint(4) NOT NULL default '0',
`thold` tinyint(4) NOT NULL default '0',
`noscore` tinyint(4) NOT NULL default '0',
`bio` tinytext,
`ublockon` tinyint(4) NOT NULL default '0',
`ublock` tinytext,
`theme` varchar(255) NOT NULL default '',
`commentmax` int(11) NOT NULL default '4096',
`counter` int(11) NOT NULL default '0',
`newsletter` tinyint(4) NOT NULL default '0',
`user_posts` int(11) NOT NULL default '0',
`user_attachsig` tinyint(4) NOT NULL default '1',
`user_rank` int(11) NOT NULL default '0',
`user_level` tinyint(4) NOT NULL default '1',
`user_active` tinyint(4) default '1',
`user_session_time` int(11) NOT NULL default '0',
`user_lastvisit` int(11) NOT NULL default '0',
`user_timezone` varchar(6) NOT NULL default '0',
`user_dst` smallint(6) NOT NULL default '0',
`user_style` tinyint(4) default NULL,
`user_lang` varchar(255) NOT NULL default 'english',
`user_dateformat` varchar(14) NOT NULL default 'D M d, Y g:i a',
`user_new_privmsg` smallint(5) unsigned NOT NULL default '0',
`user_unread_privmsg` smallint(5) unsigned NOT NULL default '0',
`user_last_privmsg` int(11) NOT NULL default '0',
`user_emailtime` int(11) default NULL,
`user_allowhtml` tinyint(4) default '1',
`user_allowbbcode` tinyint(4) default '1',
`user_allowsmile` tinyint(4) default '1',
`user_allowavatar` tinyint(4) NOT NULL default '1',
`user_allow_pm` tinyint(4) NOT NULL default '1',
`user_allow_viewonline` tinyint(4) NOT NULL default '1',
`user_notify` tinyint(4) NOT NULL default '0',
`user_notify_pm` tinyint(4) NOT NULL default '0',
`user_popup_pm` tinyint(4) NOT NULL default '0',
`user_avatar_type` tinyint(4) NOT NULL default '3',
`user_sig_bbcode_uid` varchar(10) default NULL,
`user_actkey` varchar(32) default NULL,
`user_newpasswd` varchar(32) default NULL,
`user_group_cp` int(11) NOT NULL default '2',
`user_group_list_cp` varchar(100) NOT NULL default '2',
`user_active_cp` tinyint(4) NOT NULL default '1',
`susdel_reason` text,
PRIMARY KEY (`user_id`),
KEY `uname` (`username`),
KEY `user_session_time` (`user_session_time`)
) ENGINE=MyISAM DEFAULT charset=utf8 AUTO_INCREMENT=308 ;

DROP TABLE IF EXISTS `pp_books`;
CREATE TABLE IF NOT EXISTS `pp_books` (
`book_id` smallint(4) unsigned NOT NULL auto_increment,
`book_name` varchar(50) NOT NULL default '',
`is_retired` tinyint(1) unsigned default NULL,
PRIMARY KEY (`book_id`),
UNIQUE KEY `book_name` (`book_name`)
) ENGINE=MyISAM AUTO_INCREMENT=3670 ;

DROP TABLE IF EXISTS `pp_ubooks`;
CREATE TABLE IF NOT EXISTS `pp_ubooks` (
`uid` int(10) unsigned NOT NULL default '0',
`book_id` int(10) unsigned NOT NULL default '0'
) ENGINE=MyISAM DEFAULT charset=utf8;
================================================== ==========

(The last table has a unique index which spans across both columns).

This is the query I am executing:

SELECT
t1.letter AS letter,
COUNT(*) AS total_books_in_section,
SUM(IF(t1.retired='1',1,0 )) AS total_retired,
SUM(IF(t1.retired!='1',1,0 )) AS owned_regular,
SUM(IF(t1.retired='1' AND t2.bid IS NOT NULL,1,0 )) AS
owned_retired_books,
SUM(IF(t1.retired='1' AND t2.bid IS NULL,1,0)) AS
unowned_retired_books,
SUM(IF(t1.retired!='1' AND t2.bid IS NOT NULL,1,0)) AS
owned_regular_books,
SUM(IF(t1.retired!='1' AND t2.bid IS NULL,1,0)) AS
unowned_regular_books
FROM (
SELECT
book_id AS bid,
LEFT(book_name,1) AS letter,
IF(is_retired='1',1,0) AS retired
FROM
pp_books
) AS t1
LEFT JOIN (
SELECT
book_id AS bid,
cms_users.username AS username,
IF(book_id IS NULL,0,1) AS is_owned
FROM
pp_ubooks
RIGHT JOIN
cms_users
ON
cms_users.user_id=pp_ubooks.uid
WHERE
cms_users.username='$username'
OR
cms_users.username IS NULL
) AS t2
ON
t1.bid=t2.bid
GROUP BY
letter;

I am trying to comprehend what might have gone wrong to be causing this
problem. As I mentioned before, it takes only 0.01 seconds to execute
in both phpmyadmin and at the CLI. All other queries on my website
execute super-fast, just not any that use pp_ubooks or pp_books. Even
this query takes more than 5 seconds to execute on the website, but
only 0.00 seconds using the CLI.

Just for the record, my website and the CLI are using the same
database. I think it might have something to do with my table
collations, but I can't be sure if this is the case, and if it is, why
it has such a massive impact. I have tried setting my book tables to
latin-swedish-ci (which is the default), utf8-unicode-ci, and
utf8-general-ci (which all of my other tables are set to), and nothing
seems to work any better.

I have been utterly baffled by this mystery for days now, and if
haven't got any hair left on my head to pull out anymore. If anyone
could suggest anything they believe to be helpful I would really
appreciate it! I have even gone as far as to completely scrap apache2,
mysql-server-5.0 and php4 and to start the installation all over again.
This is what leads me to beleive it could be something to do with my
tables rather than the configuration of anything in particular, but I
am just guessing.

Best wishes.

Daz.

Oh, and this is th query that takes more than 5 seconds when executed
through PHP:
SELECT book_id AS bid, LEFT(book_name,1) AS letter,
IF(is_retired='1',1,0) AS retired FROM pp_books;
Oct 28 '06 #3

P: n/a
Daz

pangea33 wrote:
How many results do you get back? If your recordset is huge, the render
time in your browser could be the problem rather than just the query.

What does php do to the records as it's outputting them? Any sort of
calculations?
The number of results varies. It can be 10, 20,30,50 or 100, and the
output is reformatted. However, I am using Dragonfly CMS, which tells
me how long the pages took to generate, and how long all of the queries
put together took to execute. I have also measured the time it takes to
execute the query myself using microtime(), and it's just taking far
too long. As I mentioned before, it never used to take as long, and
queries involving using other tables are still super-quick.

Oct 28 '06 #4

P: n/a
Daz wrote:
Oh, and this is th query that takes more than 5 seconds when executed
through PHP:
SELECT book_id AS bid, LEFT(book_name,1) AS letter,
IF(is_retired='1',1,0) AS retired FROM pp_books;
Probably a wild-goose chase ... but try

explain
SELECT book_id AS bid, LEFT(book_name,1) AS letter,
IF(is_retired='1',1,0) AS retired FROM pp_books;

both in PHP and in the CLI. See if there's any difference.

--
I (almost) never check the dodgeit address.
If you *really* need to mail me, use the address in the Reply-To
header with a message in *plain* *text* *without* *attachments*.
Oct 28 '06 #5

P: n/a
Daz

Pedro Graca wrote:
Probably a wild-goose chase ... but try

explain
SELECT book_id AS bid, LEFT(book_name,1) AS letter,
IF(is_retired='1',1,0) AS retired FROM pp_books;

both in PHP and in the CLI. See if there's any difference.
Good thinking, Pedro!

Many thanks for your suggestion, I will give it a go. I can never quite
understand what the results of EXPLAIN means, but I guess I'd better
start learning. :)

Oct 29 '06 #6

P: n/a
Daz wrote:
Hi everyone.

I was faced with the choice of whether my problem is indeed a PHP
problem or a MySQL. I have decided it's a PHP problem as I don't
experience the same problem when I execute the same query at the CLI.

I am having trouble executing a large query through my PHP script. It
takes about 7-11 seconds on
average to execute, whereas the same query only takes 0.01 seconds to
execute through the CLI.

I thought that it could have been something to do with the table
collation, and I have changed that to alsorts of different things,
still to no avail. For some reason the problem appeared to have occured
from out of the blue. It all worked fine, and then all of a sudden
started going slow. The rest of the queries executed on the database
are very fast, it only seems to be when I use the tables that I have
recently created for my project that they run slow.

I am joining three tables together:

================================================== ==========

CREATE TABLE IF NOT EXISTS `cms_users` (
`user_id` mediumint(8) unsigned NOT NULL auto_increment,
`name` varchar(60) default NULL,
`username` varchar(50) NOT NULL default '',
`user_email` varchar(255) NOT NULL default '',
`femail` varchar(255) default NULL,
`user_website` varchar(255) default NULL,
`user_avatar` varchar(255) NOT NULL default '',
`user_regdate` varchar(20) NOT NULL default '',
`user_icq` varchar(15) default NULL,
`user_occ` varchar(100) default NULL,
`user_from` varchar(100) default NULL,
`user_interests` varchar(150) default NULL,
`user_sig` varchar(255) default NULL,
`user_viewemail` tinyint(4) NOT NULL default '0',
`user_aim` varchar(35) default NULL,
`user_yim` varchar(40) default NULL,
`user_msnm` varchar(40) default NULL,
`user_password` varchar(40) NOT NULL default '',
`storynum` tinyint(4) NOT NULL default '10',
`umode` varchar(10) default NULL,
`uorder` tinyint(4) NOT NULL default '0',
`thold` tinyint(4) NOT NULL default '0',
`noscore` tinyint(4) NOT NULL default '0',
`bio` tinytext,
`ublockon` tinyint(4) NOT NULL default '0',
`ublock` tinytext,
`theme` varchar(255) NOT NULL default '',
`commentmax` int(11) NOT NULL default '4096',
`counter` int(11) NOT NULL default '0',
`newsletter` tinyint(4) NOT NULL default '0',
`user_posts` int(11) NOT NULL default '0',
`user_attachsig` tinyint(4) NOT NULL default '1',
`user_rank` int(11) NOT NULL default '0',
`user_level` tinyint(4) NOT NULL default '1',
`user_active` tinyint(4) default '1',
`user_session_time` int(11) NOT NULL default '0',
`user_lastvisit` int(11) NOT NULL default '0',
`user_timezone` varchar(6) NOT NULL default '0',
`user_dst` smallint(6) NOT NULL default '0',
`user_style` tinyint(4) default NULL,
`user_lang` varchar(255) NOT NULL default 'english',
`user_dateformat` varchar(14) NOT NULL default 'D M d, Y g:i a',
`user_new_privmsg` smallint(5) unsigned NOT NULL default '0',
`user_unread_privmsg` smallint(5) unsigned NOT NULL default '0',
`user_last_privmsg` int(11) NOT NULL default '0',
`user_emailtime` int(11) default NULL,
`user_allowhtml` tinyint(4) default '1',
`user_allowbbcode` tinyint(4) default '1',
`user_allowsmile` tinyint(4) default '1',
`user_allowavatar` tinyint(4) NOT NULL default '1',
`user_allow_pm` tinyint(4) NOT NULL default '1',
`user_allow_viewonline` tinyint(4) NOT NULL default '1',
`user_notify` tinyint(4) NOT NULL default '0',
`user_notify_pm` tinyint(4) NOT NULL default '0',
`user_popup_pm` tinyint(4) NOT NULL default '0',
`user_avatar_type` tinyint(4) NOT NULL default '3',
`user_sig_bbcode_uid` varchar(10) default NULL,
`user_actkey` varchar(32) default NULL,
`user_newpasswd` varchar(32) default NULL,
`user_group_cp` int(11) NOT NULL default '2',
`user_group_list_cp` varchar(100) NOT NULL default '2',
`user_active_cp` tinyint(4) NOT NULL default '1',
`susdel_reason` text,
PRIMARY KEY (`user_id`),
KEY `uname` (`username`),
KEY `user_session_time` (`user_session_time`)
) ENGINE=MyISAM DEFAULT charset=utf8 AUTO_INCREMENT=308 ;

DROP TABLE IF EXISTS `pp_books`;
CREATE TABLE IF NOT EXISTS `pp_books` (
`book_id` smallint(4) unsigned NOT NULL auto_increment,
`book_name` varchar(50) NOT NULL default '',
`is_retired` tinyint(1) unsigned default NULL,
PRIMARY KEY (`book_id`),
UNIQUE KEY `book_name` (`book_name`)
) ENGINE=MyISAM AUTO_INCREMENT=3670 ;

DROP TABLE IF EXISTS `pp_ubooks`;
CREATE TABLE IF NOT EXISTS `pp_ubooks` (
`uid` int(10) unsigned NOT NULL default '0',
`book_id` int(10) unsigned NOT NULL default '0'
) ENGINE=MyISAM DEFAULT charset=utf8;
================================================== ==========

(The last table has a unique index which spans across both columns).

This is the query I am executing:

SELECT
t1.letter AS letter,
COUNT(*) AS total_books_in_section,
SUM(IF(t1.retired='1',1,0 )) AS total_retired,
SUM(IF(t1.retired!='1',1,0 )) AS owned_regular,
SUM(IF(t1.retired='1' AND t2.bid IS NOT NULL,1,0 )) AS
owned_retired_books,
SUM(IF(t1.retired='1' AND t2.bid IS NULL,1,0)) AS
unowned_retired_books,
SUM(IF(t1.retired!='1' AND t2.bid IS NOT NULL,1,0)) AS
owned_regular_books,
SUM(IF(t1.retired!='1' AND t2.bid IS NULL,1,0)) AS
unowned_regular_books
FROM (
SELECT
book_id AS bid,
LEFT(book_name,1) AS letter,
IF(is_retired='1',1,0) AS retired
FROM
pp_books
) AS t1
LEFT JOIN (
SELECT
book_id AS bid,
cms_users.username AS username,
IF(book_id IS NULL,0,1) AS is_owned
FROM
pp_ubooks
RIGHT JOIN
cms_users
ON
cms_users.user_id=pp_ubooks.uid
WHERE
cms_users.username='$username'
OR
cms_users.username IS NULL
) AS t2
ON
t1.bid=t2.bid
GROUP BY
letter;

I am trying to comprehend what might have gone wrong to be causing this
problem. As I mentioned before, it takes only 0.01 seconds to execute
in both phpmyadmin and at the CLI. All other queries on my website
execute super-fast, just not any that use pp_ubooks or pp_books. Even
this query takes more than 5 seconds to execute on the website, but
only 0.00 seconds using the CLI.

Just for the record, my website and the CLI are using the same
database. I think it might have something to do with my table
collations, but I can't be sure if this is the case, and if it is, why
it has such a massive impact. I have tried setting my book tables to
latin-swedish-ci (which is the default), utf8-unicode-ci, and
utf8-general-ci (which all of my other tables are set to), and nothing
seems to work any better.

I have been utterly baffled by this mystery for days now, and if
haven't got any hair left on my head to pull out anymore. If anyone
could suggest anything they believe to be helpful I would really
appreciate it! I have even gone as far as to completely scrap apache2,
mysql-server-5.0 and php4 and to start the installation all over again.
This is what leads me to beleive it could be something to do with my
tables rather than the configuration of anything in particular, but I
am just guessing.

Best wishes.

Daz.
Daz,

Is it possible you're using an older version of the client libs with an
updated MySQL?

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Oct 29 '06 #7

P: n/a
Daz wrote:
Pedro Graca wrote:
>Probably a wild-goose chase ... but try

explain
SELECT book_id AS bid, LEFT(book_name,1) AS letter,
IF(is_retired='1',1,0) AS retired FROM pp_books;

both in PHP and in the CLI. See if there's any difference.

Many thanks for your suggestion, I will give it a go. I can never quite
understand what the results of EXPLAIN means, but I guess I'd better
start learning. :)
If there is no difference between the explains you can postpone studying
the EXPLAIN results LOL

--
I (almost) never check the dodgeit address.
If you *really* need to mail me, use the address in the Reply-To
header with a message in *plain* *text* *without* *attachments*.
Oct 29 '06 #8

P: n/a
Daz

Jerry Stuckle wrote:
Daz,

Is it possible you're using an older version of the client libs with an
updated MySQL?
I think you may be on to something there Jerry. I started getting very
impatient and guess it may have had something to do with my downgrading
to 4.2, and then back to 5.0. I stripped everything back to the bare
metal, Apache2, PHP, MySQL. phpmyadmin etc... It was tough to reinstall
everything again as I am a fairly new Linux user, and on this occassion
I had jumped through many more hoops than I needed to originally.
However, it seems to have paid off and everything is now working as it
should be. I can only assume that something was the wrong version.
Perhaps MySQL prefork?

Many thanks for your time.

Daz.

Oct 29 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.