473,387 Members | 1,678 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,387 software developers and data experts.

OT : Optimize MySQL queries

All,
I am in the process of tweaking and tuning my PHP scripts, and am starting
to look in tweaking my indexes in my MySQL tables.

I have found many, many articles on the topic and have applied quite a few
tips from them and have increased the
overall speed of the site. Question I have is, there are quite a few queries
that I have that really don't have any
"index-able" fields, with the exception of a specialized "sort" field I
guess.

Anyways, should an index of some sort (i.e. single, multiple, etc.) be
created for a query like this:
(NOTE: I read the entire result into an array, and will process it on the
PHP page, basically creating
tables and such from the results).

$sql_string = "SELECT SQL_CACHE a,b,c,d,e,f,g,h FROM mytable ORDER BY
sortid";
$tb_array = table_array(&$sql_string);

function table_array ($sql_string)
{
$result = mysql_query($sql_string) or die("bang");
$num_rows = mysql_num_rows($result);
$num_fields = mysql_num_fields($result);
$j = 0;
$x = 1;
while ($row = mysql_fetch_array($result)) {
for($j = 0;$j < $num_fields;$j++) {
$name = mysql_field_name($result, $j);
$arr[$x][$name] = $row[$name];
}
$x++;
}
return array('arr' => $arr, 'rows' => $num_rows, 'fields' => $num_fields);
}

The only index I can think of to create might be on the "sortid" field.
I don't really have a need for a WHERE clause (I don't think), since I am
returning the entire result.

Any ideas ? Thanks.
Jul 17 '05 #1
3 1923
> $sql_string = "SELECT SQL_CACHE a,b,c,d,e,f,g,h FROM mytable ORDER BY
sortid";

just thinking here and still playing w/some ideas - would it be faster if i
was to do this:

SELECT SQL_CACHE a,b,c,d,e,f,g,h FROM mytable WHERE sortid IS NOT NULL ORDER
BY sortid;

and then create an index on sortid, i.e.

CREATE INDEX sortid ON mytable (sortid);

even though i know that sortid will never be null, having an index built on
it and adding a WHERE clause may help
it out. No ? Yes ?

go easy - i'm still learning...
Jul 17 '05 #2
Hi,

On Mon, 3 May 2004 20:48:32 -0800, "StinkFinger" <st****@pinky.com>
wrote:
$sql_string = "SELECT SQL_CACHE a,b,c,d,e,f,g,h FROM mytable ORDER BY

sortid";

just thinking here and still playing w/some ideas - would it be faster if i
was to do this:

SELECT SQL_CACHE a,b,c,d,e,f,g,h FROM mytable WHERE sortid IS NOT NULL ORDER
BY sortid;

and then create an index on sortid, i.e.

CREATE INDEX sortid ON mytable (sortid);

even though i know that sortid will never be null, having an index built on
it and adding a WHERE clause may help
it out. No ? Yes ?


No. It would help if around 80% of sortid IS NULL and the rest isn't.
You read basically the whole table and database query optimizers will
decide to do a table scan typically when they believe they will touch
20% of the data.

So, your index will not be used. If you have no where clause an index
is not useful. Is there a table which you could join to your table,
ie. are you combining results with a different table?

HTH,

Jochen

--
Jochen Daum - Cabletalk Group Ltd.
PHP DB Edit Toolkit -- PHP scripts for building
database editing interfaces.
http://sourceforge.net/projects/phpdbedittk/
Jul 17 '05 #3
> No. It would help if around 80% of sortid IS NULL and the rest isn't.
You read basically the whole table and database query optimizers will
decide to do a table scan typically when they believe they will touch
20% of the data.

So, your index will not be used. If you have no where clause an index
is not useful. Is there a table which you could join to your table,
ie. are you combining results with a different table?


No, I don't have any tables to join to. Now that I am learning more and more
about MySQL and PHP, I am starting to re-evaluate my original db design.
I may end up changing alot around in the near future.

Thanks for the help.
Jul 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Philip Stoev | last post by:
Hi all, Please tell me if any of this makes sense. Any pointers to relevant projects/articles will be much appreciated. Philip Stoev http://www.stoev.org/pivot/manifest.htm ...
4
by: jy2003 | last post by:
I have read a book, which suggests we should change OR to UNION for better performance, but currently I have too many OR clauses(I have a query with 100 ORs) and it does not sound good to have 100...
0
by: Andreas Falck | last post by:
Hi, I ran the code below on two different versions, 4.0.14 and 4.0.16 respectively, both running RH 7.3 on intel. In version mysql server version 4.0.14 the SELECT privelege suffices for...
0
by: Plymouth Acclaim | last post by:
Hi guys, We have a problem with Dual AMD64 Opteron/MySQL 4.0.18/Mandrake 10 for a very high volume site. We are evaluating the performance on our new server AMD64 and it seems it's slow compared...
11
by: DJJ | last post by:
I am using the MySQL ODBC 3.51 driver to link three relatively small MySQL tables to a Microsoft Access 2003 database. I am finding that the data from the MySQL tables takes a hell of a long time...
39
by: Mairhtin O'Feannag | last post by:
Hello, I have a client (customer) who asked the question : "Why would I buy and use UDB, when MySql is free?" I had to say I was stunned. I have no experience with MySql, so I was left sort...
4
by: Huaer.XC | last post by:
>From the following MySQL command: EXPLAIN SELECT * FROM t1 JOIN t2 ON (t1.id = t2.id) JOIN t3 ON t3.name = t1.name WHERE t1.id IN(123, 124); which result is:...
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? ...
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.