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

select/update performance?

I need to maintain a manually counter for an id-field, but I can do this
two ways. Either make a counter table (which means one select and one
update) or just selecting the largest id from existing table and
increment by one (just one select + one table lock). Which one is
fastest?
Regards,

BTJ
---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postgresql.org

Nov 12 '05 #1
6 2452
Bjørn T Johansen wrote:
I need to maintain a manually counter for an id-field, but I can do this
two ways. Either make a counter table (which means one select and one
update) or just selecting the largest id from existing table and
increment by one (just one select + one table lock). Which one is
fastest?


Is would be better to create a SEQUENCE and simply call nextval on it.
Then you are assured that you'll get a unique sequence when working in a
concurrent environment.

It would also be guaranteed faster than interrogating tables.

Hope this helps,

--

Rob Fielding
Development
Designer Servers Ltd
---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 12 '05 #2
Yes, but the table in question have 3 PK and only one that needs this
"sequence" so I just thought instead of getting holes in the IDs I just
manually handle this counter somehow.. Not a big deal but... :)
BTJ

On Wed, 2003-11-05 at 10:42, Rob Fielding wrote:
Bjørn T Johansen wrote:
I need to maintain a manually counter for an id-field, but I can do this
two ways. Either make a counter table (which means one select and one
update) or just selecting the largest id from existing table and
increment by one (just one select + one table lock). Which one is
fastest?


Is would be better to create a SEQUENCE and simply call nextval on it.
Then you are assured that you'll get a unique sequence when working in a
concurrent environment.

It would also be guaranteed faster than interrogating tables.

Hope this helps,

---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to ma*******@postgresql.org)

Nov 12 '05 #3
You are buying yourself trouble. The holes in the IDs are the least
problem, and a generated sequence should not have any business meaning
anyway. BTW, what happens when you delete a record ? That would surely
leave you a hole in the IDs...
Just a quick thought: if you are going to handle manually the ID
creation, and lock rows/tables to do that, you will effectively
serialize all transactions which create ids, because the locks are held
until the transactions finish. Not to mention increased deadlock
probability.
Sequences were designed to overcome all these problems, so why not just
use them ?

Cheers,
Csaba.
On Wed, 2003-11-05 at 10:49, Bjørn T Johansen wrote:
Yes, but the table in question have 3 PK and only one that needs this
"sequence" so I just thought instead of getting holes in the IDs I just
manually handle this counter somehow.. Not a big deal but... :)
BTJ

On Wed, 2003-11-05 at 10:42, Rob Fielding wrote:
Bjørn T Johansen wrote:
I need to maintain a manually counter for an id-field, but I can do this
two ways. Either make a counter table (which means one select and one
update) or just selecting the largest id from existing table and
increment by one (just one select + one table lock). Which one is
fastest?


Is would be better to create a SEQUENCE and simply call nextval on it.
Then you are assured that you'll get a unique sequence when working in a
concurrent environment.

It would also be guaranteed faster than interrogating tables.

Hope this helps,

---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to ma*******@postgresql.org)

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 12 '05 #4
On Wednesday 05 November 2003 09:49, Bjørn T Johansen wrote:
Yes, but the table in question have 3 PK and only one that needs this
"sequence" so I just thought instead of getting holes in the IDs I just
manually handle this counter somehow.. Not a big deal but... :)


Do you mean a 3-column primary key? By definition you can't have more than one
primary key.

If you absolutely need to have no holes in your sequence numbers (e.g. for
invoices) then you will have to handle it yourself. If you can live with
them, it is much easier and quicker to use a sequence.

If you don't want holes, the simplest way is probably to have a "next_id"
column in a system-settings table. You'll need to lock it before
reading/updating and this will be a bottleneck when inserting new rows.
--
Richard Huxton
Archonet Ltd

---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postgresql.org

Nov 12 '05 #5
Bjørn T Johansen wrote:
Yes, but the table in question have 3 PK and only one that needs this
"sequence" so I just thought instead of getting holes in the IDs I just
manually handle this counter somehow.. Not a big deal but... :)


You'd only get holes if you keep making nextval requests without using
the value - say by issuing rollback. The problem with holes is actually
the feature of uniqueness SEQUENCES provides. Perhaps you judge that
there is too high a chance of rollback to create a sufficient number of
holes to warrant not using a SEQUENCE.

It's all down to your application and specific situation I guess however
your counter table idea sounds exactly like what SEQUENCE provides,
without any of the guarantees.

I think I'd still recommend using a SEQUENCE for anything but the most
profound reason :)

--

Rob Fielding
ro*@dsvr.net Development Designer Servers Ltd
---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 12 '05 #6
Ok you all, I cave... I will use sequences.... :)

BTJ

On Wed, 2003-11-05 at 11:46, Rob Fielding wrote:
Bjørn T Johansen wrote:
Yes, but the table in question have 3 PK and only one that needs this
"sequence" so I just thought instead of getting holes in the IDs I just
manually handle this counter somehow.. Not a big deal but... :)


You'd only get holes if you keep making nextval requests without using
the value - say by issuing rollback. The problem with holes is actually
the feature of uniqueness SEQUENCES provides. Perhaps you judge that
there is too high a chance of rollback to create a sufficient number of
holes to warrant not using a SEQUENCE.

It's all down to your application and specific situation I guess however
your counter table idea sounds exactly like what SEQUENCE provides,
without any of the guarantees.

I think I'd still recommend using a SEQUENCE for anything but the most
profound reason :)

---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to ma*******@postgresql.org)

Nov 12 '05 #7

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

Similar topics

16
by: lkrubner | last post by:
Are there any benchmarks on how much an extra, unneeded VARCHAR, CHAR, INT, BIGINT, TEXT or MEDIUMTEXT slows down a database call with MySql? PostGre info would also be useful. I'm trying to...
4
by: Oracle 9Ri2 AS 2.1 IA 64 | last post by:
Hello, I am working with Oracle 9Ri2 version on Linux AS 2.1 for IA64. I have a problem when i try to lanch the following program : EXEC SQL PREPARE MyStmt FROM SELECT CHAMP1 FROM TAB1...
1
by: Luc | last post by:
I am using Visual Studio 2003 and am getting lousy performance after using a datatable select and then trying to assign a value to a column of the row that was found: DataTable dt = new...
3
by: jw56578 | last post by:
Which way of retrieving a record is more effecient?: Select tbl1.field1, tbl2.field1 from table1 tbl1 inner join table2 tbl2 on tbl1.id = tbl2.id where someid = somevalue and someid =...
17
by: kalamos | last post by:
This statement fails update ded_temp a set a.balance = (select sum(b.ln_amt) from ded_temp b where a.cust_no = b.cust_no and a.ded_type_cd = b.ded_type_cd and a.chk_no = b.chk_no group by...
11
by: Michi Henning | last post by:
Hi, I'm using a blocking Select() call on a socket with a timeout value of -1. I'd expect the call to block indefinitely, but it doesn't. When I use Poll() instead, a timeout of -1 works fine...
6
by: Carlo B | last post by:
Hi I need to use a listbox to display authors from the authors table in the standard BIBLIO.MDB. I haven't even got to this point yet and the code below is bombing out on the line...
4
by: Nick Barr | last post by:
Hi, I am trying to gather stats about how many times a resource in our web app is viewed, i.e. just a COUNT. There are potentially millions of resources within the system. I thought of two...
5
by: parwal.sandeep | last post by:
Hello grp! i'm using INNODB tables which are using frequently . if i fire a SELECT query which fetch major part of table it usually take 10-20 seconds to complete. in mean time if any UPDATE...
6
by: fniles | last post by:
I am using VB.NET 2005 and Access database. My program uses a timer that kicks in every 1 min to read from a database and copy the dataset table to a datatable. This database is in a class called...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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.