spamassassin-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From David Morton <>
Subject Re: SA 3.30 question: redundant index in bayes?
Date Sat, 13 Feb 2010 16:33:08 GMT
Hash: SHA1

Matt Kettler wrote:

> A quick diff of the 3.2 and 3.3 versions of these files shows this table
> was changed:
> CREATE TABLE bayes_token (
>   id int(11) NOT NULL default '0',
>   token char(5) NOT NULL default '',
>   spam_count int(11) NOT NULL default '0',
>   ham_count int(11) NOT NULL default '0',
>   atime int(11) NOT NULL default '0',
>   PRIMARY KEY  (id, token),
>   INDEX bayes_token_idx1 (token),                    <- deleted
>   INDEX bayes_token_idx2 (id, atime)                <- renamed idx1
> So token was both a primary key, and an index, which is redundant.

How is that redundant?  If you search for only a token, it would not be
indexed, and would perform very poorly.

In section 7.4.4 of the mysql docs:

 If the table has a multiple-column index, any leftmost prefix of the
index can be used by the optimizer to find rows. For example, if you
have a three-column index on (col1, col2, col3), you have indexed search
capabilities on (col1), (col1, col2), and (col1, col2, col3).

MySQL cannot use an index if the columns do not form a leftmost prefix
of the index. Suppose that you have the SELECT statements shown here:

SELECT * FROM tbl_name WHERE col1=val1;
SELECT * FROM tbl_name WHERE col1=val1 AND col2=val2;

SELECT * FROM tbl_name WHERE col2=val2;
SELECT * FROM tbl_name WHERE col2=val2 AND col3=val3;

If an index exists on (col1, col2, col3), only the first two queries use
the index. The third and fourth queries do involve indexed columns, but
(col2) and (col2, col3) are not leftmost prefixes of (col1, col2, col3).

- --
David Morton <>

Morton Software & Design - Ruby on Rails
                                                 PHP Applications
Maia Mailguard    - Spam management
                                                 for mail servers
Version: GnuPG v1.4.7 (Darwin)
Comment: Using GnuPG with Mozilla -


View raw message