Unix Technical Forum

auto_increment

This is a discussion on auto_increment within the MySQL General forum forums, part of the MySQL category; --> hi list, reading manual on mysql regarding auto_increment with multiple-column index: CREATE TABLE animals ( grp ENUM('fish','mammal','bird') NOT NULL, ...


Go Back   Unix Technical Forum > Database Server Software > MySQL > MySQL General forum

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-24-2008, 06:08 PM
Hiep Nguyen
 
Posts: n/a
Default auto_increment

hi list,

reading manual on mysql regarding auto_increment with multiple-column
index:

CREATE TABLE animals (
grp ENUM('fish','mammal','bird') NOT NULL,
id MEDIUMINT NOT NULL AUTO_INCREMENT,
name CHAR(30) NOT NULL,
PRIMARY KEY (grp,id)
);

INSERT INTO animals (grp,name) VALUES
('mammal','dog'),('mammal','cat'),
('bird','penguin'),('fish','lax'),('mammal','whale '),
('bird','ostrich');

SELECT * FROM animals ORDER BY grp,id;

+--------+----+---------+
| grp | id | name |
+--------+----+---------+
| fish | 1 | lax |
| mammal | 1 | dog |
| mammal | 2 | cat |
| mammal | 3 | whale |
| bird | 1 | penguin |
| bird | 2 | ostrich |
+--------+----+---------+

my question is what id would be if i:

UPDATE `animals` SET `grp`='mammal' WHERE `grp`='bird' AND `id`='2' AND
`name`='ostrich' LIMIT 1;

i'm confused on auto_increment now.

thanks
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-24-2008, 06:08 PM
Sebastian Mendel
 
Posts: n/a
Default Re: auto_increment

Sebastian Mendel schrieb:
> Hiep Nguyen schrieb:
>> hi list,
>>
>> reading manual on mysql regarding auto_increment with multiple-column
>> index:
>>
>> CREATE TABLE animals (
>> grp ENUM('fish','mammal','bird') NOT NULL,
>> id MEDIUMINT NOT NULL AUTO_INCREMENT,
>> name CHAR(30) NOT NULL,
>> PRIMARY KEY (grp,id)
>> );
>>
>> INSERT INTO animals (grp,name) VALUES
>> ('mammal','dog'),('mammal','cat'),
>> ('bird','penguin'),('fish','lax'),('mammal','whale '),
>> ('bird','ostrich');
>>
>> SELECT * FROM animals ORDER BY grp,id;
>>
>> +--------+----+---------+
>> | grp | id | name |
>> +--------+----+---------+
>> | fish | 1 | lax |
>> | mammal | 1 | dog |
>> | mammal | 2 | cat |
>> | mammal | 3 | whale |
>> | bird | 1 | penguin |
>> | bird | 2 | ostrich |
>> +--------+----+---------+
>>
>> my question is what id would be if i:
>>
>> UPDATE `animals` SET `grp`='mammal' WHERE `grp`='bird' AND `id`='2'
>> AND `name`='ostrich' LIMIT 1;

>
> you do not need `name`='ostrich' in WHERE, cause grp,id are already UNIQUE
>
> your key is grp,id (bird,2)
>
> but your query will fail, because there is already grp,id (mammal,2) and
> therre can not be two identical UNIQUE (PRIMARY) keys


auto_increment comes only in effect when inserting NULL (or 0 in some SQL
mode) or nothing (with default NULL, 0 what should be always the case for
auto_increment fields)

your query should look like this:

UPDATE `animals`
SET `grp` = 'mammal',
`id` = NULL
WHERE `grp` = 'bird'
AND `id` = '2'
LIMIT 1;

--
Sebastian Mendel
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-24-2008, 06:08 PM
Ben Clewett
 
Posts: n/a
Default Re: auto_increment

Are you sure, I just get:

CREATE TABLE ...

ERROR 1075 (42000): Incorrect table definition; there can be only one
auto column and it must be defined as a key

On version 5.0.41. What version are you using?



Hiep Nguyen wrote:
> hi list,
>
> reading manual on mysql regarding auto_increment with multiple-column
> index:
>
> CREATE TABLE animals (
> grp ENUM('fish','mammal','bird') NOT NULL,
> id MEDIUMINT NOT NULL AUTO_INCREMENT,
> name CHAR(30) NOT NULL,
> PRIMARY KEY (grp,id)
> );
>
> INSERT INTO animals (grp,name) VALUES
> ('mammal','dog'),('mammal','cat'),
> ('bird','penguin'),('fish','lax'),('mammal','whale '),
> ('bird','ostrich');
>
> SELECT * FROM animals ORDER BY grp,id;
>
> +--------+----+---------+
> | grp | id | name |
> +--------+----+---------+
> | fish | 1 | lax |
> | mammal | 1 | dog |
> | mammal | 2 | cat |
> | mammal | 3 | whale |
> | bird | 1 | penguin |
> | bird | 2 | ostrich |
> +--------+----+---------+
>
> my question is what id would be if i:
>
> UPDATE `animals` SET `grp`='mammal' WHERE `grp`='bird' AND `id`='2' AND
> `name`='ostrich' LIMIT 1;
>
> i'm confused on auto_increment now.
>
> thanks
>

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 04-24-2008, 06:08 PM
Sebastian Mendel
 
Posts: n/a
Default Re: auto_increment

Hiep Nguyen schrieb:
> hi list,
>
> reading manual on mysql regarding auto_increment with multiple-column
> index:
>
> CREATE TABLE animals (
> grp ENUM('fish','mammal','bird') NOT NULL,
> id MEDIUMINT NOT NULL AUTO_INCREMENT,
> name CHAR(30) NOT NULL,
> PRIMARY KEY (grp,id)
> );
>
> INSERT INTO animals (grp,name) VALUES
> ('mammal','dog'),('mammal','cat'),
> ('bird','penguin'),('fish','lax'),('mammal','whale '),
> ('bird','ostrich');
>
> SELECT * FROM animals ORDER BY grp,id;
>
> +--------+----+---------+
> | grp | id | name |
> +--------+----+---------+
> | fish | 1 | lax |
> | mammal | 1 | dog |
> | mammal | 2 | cat |
> | mammal | 3 | whale |
> | bird | 1 | penguin |
> | bird | 2 | ostrich |
> +--------+----+---------+
>
> my question is what id would be if i:
>
> UPDATE `animals` SET `grp`='mammal' WHERE `grp`='bird' AND `id`='2' AND
> `name`='ostrich' LIMIT 1;


you do not need `name`='ostrich' in WHERE, cause grp,id are already UNIQUE

your key is grp,id (bird,2)

but your query will fail, because there is already grp,id (mammal,2) and
therre can not be two identical UNIQUE (PRIMARY) keys

--
Sebastian
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 04-24-2008, 06:08 PM
Sebastian Mendel
 
Posts: n/a
Default Re: auto_increment

Ben Clewett schrieb:
> Are you sure, I just get:
>
> CREATE TABLE ...
>
> ERROR 1075 (42000): Incorrect table definition; there can be only one
> auto column and it must be defined as a key


the mentioned CREATE TABLE is fine and works


> On version 5.0.41. What version are you using?


this works on all versions, and the example is from the MySQL manual

http://dev.mysql.com/doc/refman/5.1/...increment.html

--
Sebastian Mendel
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 04-24-2008, 06:08 PM
Ben Clewett
 
Posts: n/a
Default Re: auto_increment


You are right, I've tried 5.0.18 and 5.0.45 which work.

There must have been a bug in 5.0.41 with which I used test the question...

I belive the question has been answered by now anyway

Ben


Sebastian Mendel wrote:
> Ben Clewett schrieb:
>> Are you sure, I just get:
>>
>> CREATE TABLE ...
>>
>> ERROR 1075 (42000): Incorrect table definition; there can be only one
>> auto column and it must be defined as a key

>
> the mentioned CREATE TABLE is fine and works
>
>
>> On version 5.0.41. What version are you using?

>
> this works on all versions, and the example is from the MySQL manual
>
> http://dev.mysql.com/doc/refman/5.1/...increment.html
>

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 04-24-2008, 06:08 PM
Hiep Nguyen
 
Posts: n/a
Default Re: auto_increment

On Tue, 22 Apr 2008, Sebastian Mendel wrote:

> Sebastian Mendel schrieb:
>> Hiep Nguyen schrieb:
>>> hi list,
>>>
>>> reading manual on mysql regarding auto_increment with multiple-column
>>> index:
>>>
>>> CREATE TABLE animals (
>>> grp ENUM('fish','mammal','bird') NOT NULL,
>>> id MEDIUMINT NOT NULL AUTO_INCREMENT,
>>> name CHAR(30) NOT NULL,
>>> PRIMARY KEY (grp,id)
>>> );
>>>
>>> INSERT INTO animals (grp,name) VALUES
>>> ('mammal','dog'),('mammal','cat'),
>>> ('bird','penguin'),('fish','lax'),('mammal','whale '),
>>> ('bird','ostrich');
>>>
>>> SELECT * FROM animals ORDER BY grp,id;
>>>
>>> +--------+----+---------+
>>> | grp | id | name |
>>> +--------+----+---------+
>>> | fish | 1 | lax |
>>> | mammal | 1 | dog |
>>> | mammal | 2 | cat |
>>> | mammal | 3 | whale |
>>> | bird | 1 | penguin |
>>> | bird | 2 | ostrich |
>>> +--------+----+---------+
>>>
>>> my question is what id would be if i:
>>>
>>> UPDATE `animals` SET `grp`='mammal' WHERE `grp`='bird' AND `id`='2' AND
>>> `name`='ostrich' LIMIT 1;

>>
>> you do not need `name`='ostrich' in WHERE, cause grp,id are already UNIQUE
>>
>> your key is grp,id (bird,2)
>>
>> but your query will fail, because there is already grp,id (mammal,2) and
>> therre can not be two identical UNIQUE (PRIMARY) keys

>
> auto_increment comes only in effect when inserting NULL (or 0 in some SQL
> mode) or nothing (with default NULL, 0 what should be always the case for
> auto_increment fields)
>
> your query should look like this:
>
> UPDATE `animals`
> SET `grp` = 'mammal',
> `id` = NULL
> WHERE `grp` = 'bird'
> AND `id` = '2'
> LIMIT 1;
>
> --
> Sebastian Mendel
>


thanks, i got it.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 04-24-2008, 06:08 PM
=?ISO-8859-1?Q?Bruno_B . _B . _Magalh=E3es?=
 
Posts: n/a
Default Performance

Hi everybody,

I am back to this list after a long period away due to work time
restrictions... I have great news and a few interesting applications
that I will release to the mysql community very soon, most probably as
open source.

But now I have a performance problem with a client of mine, that I was
not able to solve... The problem is that I have a very large table in
terms of data, about 7.000.000 financial transactions records, with
the following table (translated from portuguese):

CREATE TABLE `transactions` (
`client_id` int(5) unsigned zerofill NOT NULL default '00000',
`client_unit_id` int(4) unsigned zerofill NOT NULL default '0000',
`client_property_id` int(6) unsigned zerofill NOT NULL default
'000000',
`transaction_id` int(6) unsigned zerofill NOT NULL default '000000',
`transaction_account_id` int(3) unsigned zerofill NOT NULL default
'000',
`transaction_classification_id` int(3) unsigned NOT NULL default '0',
`transaction_category_id` int(4) unsigned zerofill NOT NULL default
'0000',
`transaction_complement` varchar(200) NOT NULL,
`transaction_date` date default NULL,
`transaction_amount` decimal(16,2) NOT NULL,
`transaction_parcel` varchar(8) NOT NULL,
`transaction_nature` varchar(1) NOT NULL
KEY `transactions_idx_1`
(`
client_id
`,`client_unit_id`,`client_property_id`,`transacti on_account_id`,

`
transaction_classification_id
`
,`
transaction_category_id
`,`transaction_id`,`transaction_date`,`transaction _nature`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8

And most the queries are similar to this one:

SELECT UNIX_TIMESTAMP(transactions.transaction_date) AS date,
transactions.transaction_complement AS complement,
transactions.transaction_parcel AS parcel,
transactions.transaction_amount AS amount,
transactions.transaction_nature AS nature,
transactions_categories.transaction_category_descr iption AS
category_description
FROM transactions AS transactions
LEFT JOIN transactions_categories AS transactions_categories
ON transactions.transaction_category_id =
transactions_categories.transaction_category_id
WHERE transactions.client_id = :client
AND transactions.client_unit_id = :unit
AND transactions.transaction_date >= :start_date
AND transactions.transaction_date <= :stop_date
ORDER BY transactions.transaction_date,
transactions.transaction_id ASC

So the most important indexes are client_id , client_unit_id ,
client_property_id , transaction_account_id ,
transaction_classification_id , transaction_category_id ,
transaction_id , transaction_date , transaction_nature, and most of
the time they are called together, I thing the most problematic part
of those queries are the date range part, should I use a different
index only for this column to maintain the index small?

Most of the financials reports today takes about 8 to 12 seconds to be
generated for one month (course that I have to sum previous months
totals to give the balance).

Thanks in advance...

Regards,
Bruno B B Magalh'aes
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 04-24-2008, 06:08 PM
Phil
 
Posts: n/a
Default Re: Performance

I'm sure if you created an index on
client_id,client_unit_id,transaction_date (with optionally something else to
make unique) it would increase performance.

What does an EXPLAIN give you?

Phil

On Tue, Apr 22, 2008 at 11:41 AM, Bruno B. B. Magalhães <
brunomagalhaes@blackbean.com.br> wrote:

> Hi everybody,
>
> I am back to this list after a long period away due to work time
> restrictions... I have great news and a few interesting applications thatI
> will release to the mysql community very soon, most probably as open source.
>
> But now I have a performance problem with a client of mine, that I was not
> able to solve... The problem is that I have a very large table in terms of
> data, about 7.000.000 financial transactions records, with the following
> table (translated from portuguese):
>
> CREATE TABLE `transactions` (
> `client_id` int(5) unsigned zerofill NOT NULL default '00000',
> `client_unit_id` int(4) unsigned zerofill NOT NULL default '0000',
> `client_property_id` int(6) unsigned zerofill NOT NULL default '000000',
> `transaction_id` int(6) unsigned zerofill NOT NULL default '000000',
> `transaction_account_id` int(3) unsigned zerofill NOT NULL default '000',
> `transaction_classification_id` int(3) unsigned NOT NULL default '0',
> `transaction_category_id` int(4) unsigned zerofill NOT NULL default
> '0000',
> `transaction_complement` varchar(200) NOT NULL,
> `transaction_date` date default NULL,
> `transaction_amount` decimal(16,2) NOT NULL,
> `transaction_parcel` varchar(8) NOT NULL,
> `transaction_nature` varchar(1) NOT NULL
> KEY `transactions_idx_1`
> (`client_id`,`client_unit_id`,`client_property_id` ,`transaction_account_id`,
>
> `transaction_classification_id`,`transaction_categ ory_id`,`transaction_id`,`transaction_date`,`trans action_nature`)
> ) ENGINE=MyISAM DEFAULT CHARSET=utf8
>
> And most the queries are similar to this one:
>
> SELECT UNIX_TIMESTAMP(transactions.transaction_date) AS date,
> transactions.transaction_complement AS complement,
> transactions.transaction_parcel AS parcel,
> transactions.transaction_amount AS amount,
> transactions.transaction_nature AS nature,
> transactions_categories.transaction_category_descr iption
> AS category_description
> FROM transactions AS transactions
> LEFT JOIN transactions_categories AS transactions_categories
> ON transactions.transaction_category_id =
> transactions_categories.transaction_category_id
> WHERE transactions.client_id = :client
> AND transactions.client_unit_id = :unit
> AND transactions.transaction_date >= :start_date
> AND transactions.transaction_date <= :stop_date
> ORDER BY transactions.transaction_date,
> transactions.transaction_id ASC
>
> So the most important indexes are client_id , client_unit_id ,
> client_property_id , transaction_account_id , transaction_classification_id
> , transaction_category_id , transaction_id , transaction_date ,
> transaction_nature, and most of the time they are called together, I thing
> the most problematic part of those queries are the date range part, should I
> use a different index only for this column to maintain the index small?
>
> Most of the financials reports today takes about 8 to 12 seconds to be
> generated for one month (course that I have to sum previous months totalsto
> give the balance).
>
> Thanks in advance...
>
> Regards,
> Bruno B B Magalh'aes
>
> --
> MySQL General Mailing List
> For list archives: http://lists.mysql.com/mysql
> To unsubscribe: http://lists.mysql.com/mysql?unsub=pchapman@nc.rr.com
>




--
Help build our city at http://free-dc.myminicity.com !

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 04-24-2008, 06:08 PM
Rob Wultsch
 
Posts: n/a
Default Re: Performance

On Tue, Apr 22, 2008 at 8:41 AM, Bruno B. B. Magalhães <
brunomagalhaes@blackbean.com.br> wrote:

> Hi everybody,
>
> I am back to this list after a long period away due to work time
> restrictions... I have great news and a few interesting applications thatI
> will release to the mysql community very soon, most probably as open source.
>
> But now I have a performance problem with a client of mine, that I was not
> able to solve... The problem is that I have a very large table in terms of
> data, about 7.000.000 financial transactions records, with the following
> table (translated from portuguese):
>
> CREATE TABLE `transactions` (
> `client_id` int(5) unsigned zerofill NOT NULL default '00000',
> `client_unit_id` int(4) unsigned zerofill NOT NULL default '0000',
> `client_property_id` int(6) unsigned zerofill NOT NULL default '000000',
> `transaction_id` int(6) unsigned zerofill NOT NULL default '000000',
> `transaction_account_id` int(3) unsigned zerofill NOT NULL default '000',
> `transaction_classification_id` int(3) unsigned NOT NULL default '0',
> `transaction_category_id` int(4) unsigned zerofill NOT NULL default
> '0000',
> `transaction_complement` varchar(200) NOT NULL,
> `transaction_date` date default NULL,
> `transaction_amount` decimal(16,2) NOT NULL,
> `transaction_parcel` varchar(8) NOT NULL,
> `transaction_nature` varchar(1) NOT NULL
> KEY `transactions_idx_1`
> (`client_id`,`client_unit_id`,`client_property_id` ,`transaction_account_id`,
>
> `transaction_classification_id`,`transaction_categ ory_id`,`transaction_id`,`transaction_date`,`trans action_nature`)
> ) ENGINE=MyISAM DEFAULT CHARSET=utf8
>
> And most the queries are similar to this one:
>
> SELECT UNIX_TIMESTAMP(transactions.transaction_date) AS date,
> transactions.transaction_complement AS complement,
> transactions.transaction_parcel AS parcel,
> transactions.transaction_amount AS amount,
> transactions.transaction_nature AS nature,
> transactions_categories.transaction_category_descr iption
> AS category_description
> FROM transactions AS transactions
> LEFT JOIN transactions_categories AS transactions_categories
> ON transactions.transaction_category_id =
> transactions_categories.transaction_category_id
> WHERE transactions.client_id = :client
> AND transactions.client_unit_id = :unit
> AND transactions.transaction_date >= :start_date
> AND transactions.transaction_date <= :stop_date
> ORDER BY transactions.transaction_date,
> transactions.transaction_id ASC
>
> So the most important indexes are client_id , client_unit_id ,
> client_property_id , transaction_account_id , transaction_classification_id
> , transaction_category_id , transaction_id , transaction_date ,
> transaction_nature, and most of the time they are called together, I thing
> the most problematic part of those queries are the date range part, should I
> use a different index only for this column to maintain the index small?
>
> Most of the financials reports today takes about 8 to 12 seconds to be
> generated for one month (course that I have to sum previous months totalsto
> give the balance).
>
> Thanks in advance...
>
> Regards,
> Bruno B B Magalh'aes
>
> --
> MySQL General Mailing List
> For list archives: http://lists.mysql.com/mysql
> To unsubscribe: http://lists.mysql.com/mysql?unsub=wultsch@gmail.com
>
> I would think probably not on the date range. An Phil says an EXPLAIN

would probably be helpful. Particularly of note would be the key_len. I
would image an optimal index would be something like
KEY `transactions_idx_1` (`client_id`,`client_unit_id`,`transaction_date`)

If memory serves MySQL does not use a composite index to the right of range
scan.


--
Rob Wultsch
wultsch@gmail.com
wultsch (aim)

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 02:26 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com