If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > General > Database Concepts & Design > db keys and efficiency

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-02-12, 22:42
johnrc1 johnrc1 is offline
Registered User
 
Join Date: Jul 2012
Posts: 2
db keys and efficiency

Hi,
I am making a db using sqlite3 with python , but my question is pretty general I think even if you don't know this db system you can probably help me. I am trying to decide what the best key(s) are to use in a table. Here is what it might look like using the common tutorial analogy:

Customer, OrderDate
A, 2012-1-1
A, 2012-1-2
B, 2012-1-1
B, 2012-1-2

In my db there could be 50 customers but each customer could in extreme cases have 10000 OrderDates! My application will repeatedly be selecting all the orders from one customer between a date range.

Is there any way that I can set up the primary keys so that this type of query will be more efficient? Customer + OrderDate will be unique, will there be any advantage of making these combined columns the primary key since I will only query for date ranges and not specific dates?

My expectation is not that this will run in seconds but I would like to avoid taking 30 minutes if it could take 5...

Thanks so much for your help!
Reply With Quote
  #2 (permalink)  
Old 07-02-12, 23:56
Pat Phelan Pat Phelan is online now
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 13,574
Switch the order of the columns, use OrderDate then Customer to get the best of both worlds!

-PatP
__________________
In theory, theory and practice are identical. In practice, theory and practice are unrelated.
Reply With Quote
  #3 (permalink)  
Old 07-03-12, 07:54
johnrc1 johnrc1 is offline
Registered User
 
Join Date: Jul 2012
Posts: 2
Quote:
Switch the order of the columns, use OrderDate then Customer to get the best of both worlds!
Thanks Pat! Could you explain why this would help?
Reply With Quote
Reply

Tags
date, date ranges, key, primary key, sqlite3

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On