# mySQL newbie seek help with db indexing

**URL:** <https://www.ruby-forum.com/t/mysql-newbie-seek-help-with-db-indexing/156082>\
**Category:** Rails\
**Created:** [December 20, 2008, 7:01am UTC](https://www.ruby-forum.com/t/mysql-newbie-seek-help-with-db-indexing/156082 "2008-12-20T07:01:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeffpritchard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/43a26b/32.png) [@jeffpritchard](https://www.ruby-forum.com/u/jeffpritchard)\
**Post date:** [December 20, 2008, 7:01am UTC](https://www.ruby-forum.com/t/mysql-newbie-seek-help-with-db-indexing/156082/1 "2008-12-20T07:01:48Z")

</div>

Hi,  
I don’t quite understand the syntax for database indexes and/or how the  
work to help speed up specific database searches. Can somebody suggest  
a set of indexs that would make this set of queries go faster?

Contact Load (0.001406) SELECT \* FROM `contacts` WHERE  
(`contacts`.user\_id = 1)  
Phone Load (0.003268) SELECT \* FROM `phones` WHERE  
(`phones`.contact\_id = 8485)  
Email Load (0.000371) SELECT \* FROM `emails` WHERE  
(`emails`.contact\_id = 8485)  
Note Load (0.019782) SELECT \* FROM `notes` WHERE (`notes`.contact\_id  
= 8485)  
Group Load (0.000154) SELECT `groups`.\* FROM `groups` INNER JOIN  
groupers ON groups.id = groupers.group\_id WHERE ((`groupers`.contact\_id  
= 8486))

a Contact has\_many phones, emails, notes.  
a Contact has\_many groups :through groupers

Thanks much, and thanks even more if you include some explanation!

thanks,  
jp

---

<div class="post-metadata">

**Author:** ![jeffpritchard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/43a26b/32.png) [@jeffpritchard](https://www.ruby-forum.com/u/jeffpritchard)\
**Post date:** [December 20, 2008, 8:12am UTC](https://www.ruby-forum.com/t/mysql-newbie-seek-help-with-db-indexing/156082/2 "2008-12-20T08:12:11Z")

</div>

On Dec 20, 1:01 am, Jeff P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Hi,  
> I don’t quite understand the syntax for database indexes and/or how the  
> work to help speed up specific database searches. Can somebody suggest  
> a set of indexs that would make this set of queries go faster?  
> […]

Very simple: you can profit by indexing on almost anything you’re  
going to be doing a lot of searching on. In this case, that means  
that you should consider building indices on your foreign key fields  
(such as contacts.user\_id), since they’re what you’re searching on in  
these queries. But before you do that, you might want to try EXPLAIN  
SELECT on the queries you’re interested in, and see what mySQL tells  
you about where the bottlenecks are.

## Best,

Marnen Laibow-Koser  
removed\_email\_address@domain.invalid  
[http://www.marnen.org](http://www.marnen.org)

---

<div class="post-metadata">

**Author:** ![jeffpritchard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/43a26b/32.png) [@jeffpritchard](https://www.ruby-forum.com/u/jeffpritchard)\
**Post date:** [December 20, 2008, 12:33pm UTC](https://www.ruby-forum.com/t/mysql-newbie-seek-help-with-db-indexing/156082/3 "2008-12-20T12:33:36Z")

</div>

On 20 Dec 2008, at 07:11, Marnen Laibow-Koser wrote:

> […]
> 
> Very simple: you can profit by indexing on almost anything you’re  
> going to be doing a lot of searching on. In this case, that means  
> that you should consider building indices on your foreign key fields  
> (such as contacts.user\_id), since they’re what you’re searching on in  
> these queries. But before you do that, you might want to try EXPLAIN  
> SELECT on the queries you’re interested in, and see what mySQL tells  
> you about where the bottlenecks are.

All of the above is good. To explain a little more if you do

SELECT \* from contacts where contacts.user\_id = 2356

and there is no index then the database must examine every single row  
in the contacts table to see if it has the right user\_id whereas if it  
has an index it can go straight there. (similar to how if you have a  
phone book and you’re looking for all entries ‘Michael smith’ you  
don’t have to read the entire phone book, you can jump to the  
appropriate page very quickly)

Fred

---

<div class="post-metadata">

**Author:** ![jeffpritchard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/43a26b/32.png) [@jeffpritchard](https://www.ruby-forum.com/u/jeffpritchard)\
**Post date:** [December 20, 2008, 5:47pm UTC](https://www.ruby-forum.com/t/mysql-newbie-seek-help-with-db-indexing/156082/4 "2008-12-20T17:47:38Z")

</div>

On 20 Dec 2008, at 16:45, Jeff P. wrote:

> > and there is no index then the database must examine every single row  
> > in the contacts table to see if it has the right user\_id whereas if  
> > it  
> > has an index it can go straight there. (similar to how if you have a
> > 
> > Fred
> 
> Thanks Marnen and Fred,  
> Does the same logic completely cover joins  
> Yup

Fred

---

<div class="post-metadata">

**Author:** ![jeffpritchard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/43a26b/32.png) [@jeffpritchard](https://www.ruby-forum.com/u/jeffpritchard)\
**Post date:** [December 20, 2008, 5:45pm UTC](https://www.ruby-forum.com/t/mysql-newbie-seek-help-with-db-indexing/156082/5 "2008-12-20T17:45:35Z")

</div>

Frederick C. wrote:

> On 20 Dec 2008, at 07:11, Marnen Laibow-Koser wrote:
> 
> > […]
> > 
> > Very simple: you can profit by indexing on almost anything you’re  
> > All of the above is good. To explain a little more if you do
> 
> SELECT \* from contacts where contacts.user\_id = 2356
> 
> and there is no index then the database must examine every single row  
> in the contacts table to see if it has the right user\_id whereas if it  
> has an index it can go straight there. (similar to how if you have a
> 
> Fred

Thanks Marnen and Fred,  
Does the same logic completely cover joins, or is there something  
special to consider for those?

thanks,  
jp
