Normal Topic Unique Constraints and Compound Keys (Read 1497 times)
MP
Full Member
***
Offline



Posts: 104
Joined: Sep 3rd, 2007
Unique Constraints and Compound Keys
Oct 7th, 2007 at 3:01pm
Print Post Print Post  
It appears that the only way to create a unique constraint that spans multiple fields is to create a separate field to store a compound key and manually concatenate the fields which compose the key.  Is this correct?

Also, are unique constraints enforced when calling XResultSetValue?
  
Back to top
 
IP Logged
 
Hammer
YaBB Administrator
Lanticans
*****
Offline


Fire bad. Tree pretty.

Posts: 3436
Location: Ohio
Joined: Nov 22nd, 2002
Re: Unique Constraints and Compound Keys
Reply #1 - Oct 7th, 2007 at 3:17pm
Print Post Print Post  
MP wrote on Oct 7th, 2007 at 3:01pm:
It appears that the only way to create a unique constraint that spans multiple fields is to create a separate field to store a compound key and manually concatenate the fields which compose the key.  Is this correct?

Depends on how you want to use it. If you are trying to emulate a relational-style multiple field primary key, then yes.

Quote:
Also, are unique constraints enforced when calling XResultSetValue?

No. You can use XResultSetValue to override.
  

- Hammer
The plural of anecdote is not data.
Back to top
IP Logged
 
MP
Full Member
***
Offline



Posts: 104
Joined: Sep 3rd, 2007
Re: Unique Constraints and Compound Keys
Reply #2 - Nov 15th, 2007 at 2:39am
Print Post Print Post  
Is there a performance advantage to using @IsUnique with a compound key versus using @XResultSetSearch with multiple criteria?

On a related note, are @XResultSetSearch calls also optimized by SERVER PREINDEX XLOOKUPS?

(Further aside, it would be really useful if Lantica released an "internals" white paper.  Knowing how things like storage and indexing and locking work at a low level is key to truly allowing developers to milk the best performance out of a DB.  As is, performance is a great mystery in Sesame, making it hard to answer questions like "How do I optimize a query?" and "When are the PREINDEX indexes rebuilt?".)
  
Back to top
 
IP Logged
 
Hammer
YaBB Administrator
Lanticans
*****
Offline


Fire bad. Tree pretty.

Posts: 3436
Location: Ohio
Joined: Nov 22nd, 2002
Re: Unique Constraints and Compound Keys
Reply #3 - Nov 15th, 2007 at 3:34am
Print Post Print Post  
MP wrote on Nov 15th, 2007 at 2:39am:
Is there a performance advantage to using @IsUnique with a compound key versus using @XResultSetSearch with multiple criteria?

If you are doing what I think you are, @IsUnique should be faster. Single value comparison, stops on first match. The @XResultSetSearch method requires multiple comparisons and will continue looking until it finds all matches.

Quote:
On a related note, are @XResultSetSearch calls also optimized by SERVER PREINDEX XLOOKUPS?

I think so, but I'm not where I can check. I'll confirm in the morning.

Quote:
(Further aside, it would be really useful if Lantica released an "internals" white paper.  Knowing how things like storage and indexing and locking work at a low level is key to truly allowing developers to milk the best performance out of a DB.  As is, performance is a great mystery in Sesame, making it hard to answer questions like "How do I optimize a query?" and "When are the PREINDEX indexes rebuilt?".)

Honestly, MP, this very rarely comes up. The vast majority of our user base doesn't have any need (or desire) for that level of milking and optimization. The only time we hear this kind of question is when pro developers come at us from something like Oracle and haven't shaken off the big engine mindset. We are designed for a different environment and a different kind of user than Oracle and SQL Server and such.

The answers to these questions are usually pretty straightforward and short enough to where white papers are kind of overkill.

I'll confirm this in the morning, but I believe the PREINDEX index for a field is built the first time per session that the server uses that field value as the key value in an X command. Once an index exists, it is updated when indexed values are changed/added/deleted. So, the first X command per session using a particular key field will be slower than all subsequent X commands using that key field. PREINDEX speed increases only apply to absolute key values like "APPLE".  Keys including a search spec, like "..APPLE.." do not benefit. Unloading a database also unloads the indexes for that database.

Optimizing queries involves some very basic rules, some of which are common sense to someone with your experience:
1. Absolute searches are faster than searches with wildcards or ranges.
2. Programmed retrieve specs are slower than non-programmed. Wherever possible, pre-resolve constant values like @Date in a retrieve spec by using the pipe (|) syntax to gain significant speed over the {} equivalent.
3. For subrecord searches, Natural linking is faster than Relational linking.

You're not dealing with JOINs or HAVING clauses or nested SELECTS or any of that kind of stuff. To put this in SQL parlance (although we are not SQL-based), you have the equivalent of SELECT, WHERE and ORDER BY with no nesting. Subrecords add a level to that under-the-hood, but you get my drift. It's a very straightforward query environment. 

We maintain a constant presence among our users, so we are constantly adding tools and making adjustments to optimize Sesame to the way it is actually being used in the field. If you have questions about specific things, the developers and Support people are here and willing to help, or you can contact Support directly. Also, there is a section in the back of the User Guide that discusses some of our internal structures and may provide some of the information you are after.

Edited:
MP: I checked on it this morning and the statements I made above regarding preindexing are accurate.
« Last Edit: Nov 15th, 2007 at 3:27pm by Hammer »  

- Hammer
The plural of anecdote is not data.
Back to top
IP Logged
 
pineberry
Full Member
***
Offline


No personal text

Posts: 115
Location: Strawberry, AZ  USA
Joined: Nov 30th, 2002
Re: Unique Constraints and Compound Keys
Reply #4 - Nov 15th, 2007 at 4:41am
Print Post Print Post  
Great response. Lantica continues their fabulous support. In other languages or systems I have written various routines to test the different approaches to time operations and I think that could be done with Sesame's extensive set of tools if timing is critical.
  
Back to top
 
IP Logged
 
MP
Full Member
***
Offline



Posts: 104
Joined: Sep 3rd, 2007
Re: Unique Constraints and Compound Keys
Reply #5 - Nov 15th, 2007 at 9:11pm
Print Post Print Post  
Hammer wrote on Nov 15th, 2007 at 3:34am:
The vast majority of our user base doesn't have any need (or desire) for that level of milking and optimization.

Thank you for the detailed and informative response.  I freely admit to being a (graduate?) student of the "big engine mindset".  But even if that wasn't true, regardless of the programming task at hand, I prefer to know as much about the internals as possible so that the answer to "Why is this slow?" isn't that I didn't program it efficiently the first time.  Not that I've had that question asked about my Sesame application yet (which, BTW, is still in BETA).  I'm just trying to pro-actively keep those questions at bay.

Your response does inspire one more question.  I try to avoid any functions that rely on a form, such as XLOOKUP.  I understand their genesis, but they still rub me wrong.  I prefer to utilize the data abstraction layer that is available via the XResultSet functions.  But a key (possible) advantage in the XLOOKUP world is @XLookupSourceList.  In a client/server environment, this method clearly does everything in a single fetch.  The same may or may not be true in the XResultSet world, thus leading to the following question:

Do XResultSetValue and @XResultSetValue impose a network cost in a Client/Server environment, or are they working with a cached result set on the client side?  And, if each @XResultSetValue does a single write/commit across the network, is there a more efficient way to batch together a set of updates to a ResultSet?

PS: The Philosophy and Advanced Concepts appendixes were the first things I read when evaluating your product.  They gave me the confidence that this was a product worth exploring further.
  
Back to top
 
IP Logged
 
Hammer
YaBB Administrator
Lanticans
*****
Offline


Fire bad. Tree pretty.

Posts: 3436
Location: Ohio
Joined: Nov 22nd, 2002
Re: Unique Constraints and Compound Keys
Reply #6 - Nov 15th, 2007 at 9:38pm
Print Post Print Post  
MP wrote on Nov 15th, 2007 at 9:11pm:
Your response does inspire one more question.  I try to avoid any functions that rely on a form, such as XLOOKUP.  I understand their genesis, but they still rub me wrong.  I prefer to utilize the data abstraction layer that is available via the XResultSet functions.

No, you don't. At least not in every situation.  Smiley

Sesame is form-based. We do everything we can to avoid our users having to know about anything like a "data abstraction layer". They know about insurance and trucking and policework. They tend to know little or nothing about formal database design and concepts. We are designed to support them without forcing them to deal with too many abstractions. It's not a case of XResultSet good, XLookup bad. XResultSet is better for some tasks, XLookup (and related) is better for others.

Quote:
But a key (possible) advantage in the XLOOKUP world is @XLookupSourceList.

Exactly my point.
X commands you might find useful that should not offend your sensibilities if used appropriately:
@XLookup
@XLookupSourceList
@XLookupSourceListAll
@XListValues
@XLookupAll

If they are not on the list above and are not in the XResultSet family, they are present for Q&A compatibility and are not the best choice available to you.

Quote:
In a client/server environment, this method clearly does everything in a single fetch.

Yes. BTW, you might be tempted to make the same conclusion about XPost. Don't. XPost only exists for Q&A compatibility. Always use XResultSet to update an external record.

Quote:
The same may or may not be true in the XResultSet world, thus leading to the following question:

Do XResultSetValue and @XResultSetValue impose a network cost in a Client/Server environment, or are they working with a cached result set on the client side?

They are not working with a cached client-side result set. They are working with a cached server-side list of pointers into something we internally call the "all" list. This is a list of pointers to all the data records in the application.   

Quote:
And, if each @XResultSetValue does a single write/commit across the network, is there a more efficient way to batch together a set of updates to a ResultSet?

No. This is the most efficient way to update a record in code. It sends  the instruction to update the current record for the specified result set, the affected field name and the new value. The actual work is done server-side. You are not pumping the entire updated record across the net with each update. 

Quote:
PS: The Philosophy and Advanced Concepts appendixes were the first things I read when evaluating your product.  They gave me the confidence that this was a product worth exploring further.

We are happy to provide more detail as needed.
  

- Hammer
The plural of anecdote is not data.
Back to top
IP Logged