473,321 Members | 1,778 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,321 software developers and data experts.

Problem with executing MySQL queries through PHP.

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
8 1991
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Ganbold | last post by:
Hi, I'm using MySQL 4.0.13 for both master and slave servers. However I have problem starting slave server. Error log says: 030804 14:06:10 Slave I/O thread: connected to master...
0
by: Daniel Crespo | last post by:
Hi to all, I'm using adodb for accessing mysql and postgres. My problem relies on the mysql access. Sometimes, when I try to execute a query (using ExecTrans method below), I get this error:...
3
by: Juan Antonio Villa | last post by:
Hello, I'm having a problem replicating a simple database using the binary log replication, here is the problem: When the master sends an update to the slave, an example update reads as follows:...
1
by: PowerLifter1450 | last post by:
I've been having a very rough time installinig mySQL on Linux. I have been following the instructions form here: http://www.hostlibrary.com/installing_apache_mysql_php_on_linux Everytime I get to...
4
by: Federico | last post by:
Hi everybody, I'm evaluating the possibility of using MySQL 5.0 as a database backend for an application we are doing and I'd like to have hardware requirements rough estimates and/or real world...
29
by: wizofaus | last post by:
I previously posted about a problem where it seemed that changing the case of the word "BY" in a SELECT query was causing it to run much much faster. Now I've hit the same thing again, where...
13
by: Ciaran | last post by:
Hi All, Is it faster to have mySql look up as much data as possible in one complex query or to have php do all the complex processing and submit lots of simple queries to the mysql database? ...
30
by: Einstein30000 | last post by:
Hi, in one of my php-scripts is the following query (with an already open db-connection): $q = "INSERT INTO main (name, img, descr, from, size, format, cat, host, link, date) VALUES ('$name',...
6
Atli
by: Atli | last post by:
This is an easy to digest 12 step guide on basics of using MySQL. It's a great refresher for those who need it and it work's great for first time MySQL users. Anyone should be able to get...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.