<p>We are working on designing an application that is typically OLTP (think: purchasing system). However, this one in particular has the need that some users will be offline, so they need to be able to download the DB to their machine, work on it, and then sync back once they're on the LAN.</p>

<p>I would like to note that I know this has been done before, I just don't have experience with this particular model.</p>

<p>One idea I thought about was using GUIDs as table keys. So for example, a Purchase Order would not have a number (auto-numeric) but a GUID instead, so that every offline client can generate those, and I don't have clashes when I connect back to the DB.</p>

<p>Is this a bad idea for some reason?
Will access to these tables through the GUID key be slow?</p>

<p>Have you had experience with these type of systems? How have you solved this problem?</p>

<p>Thanks!<br>
Daniel</p>

[{'answer_id': 40249, 'author': 'Rob Walker', 'author_id': 3631, 'author_profile': 'https://Stackoverflow.com/users/3631', 'pm_score': 0, 'selected': False, 'text': '<p>Guids will certainly be slower (and use more memory) than standard integer keys, but whether or not that is an issue will depend on the type of load your system will see.  Depending on your backend DB there may be issues with indexing guid fields.</p>\n\n<p>Using guids simplifies a whole class of problems, but you pay for it part will performance and also debuggability - typing guids into those test queries will get old real fast!</p>\n'}, {'answer_id': 40253, 'author': 'Michael Haren', 'author_id': 29, 'author_profile': 'https://Stackoverflow.com/users/29', 'pm_score': 2, 'selected': False, 'text': "<p>This is a perfectly good use of GUIDs. The only draw backs would be a slight complexity in working with GUIDs over INTs and the slight size difference (16 bytes vs 4 bytes).</p>\n\n<p>I don't think either of those are a big deal.</p>\n"}, {'answer_id': 40275, 'author': 'Daniel Magliola', 'author_id': 3314, 'author_profile': 'https://Stackoverflow.com/users/3314', 'pm_score': 0, 'selected': False, 'text': '<p>The backend will be SQL Server 2005<br>\nFrontend / Application Logic will be .Net</p>\n\n<p>Besides GUIDs, can you think of other ways to resolve the "merge" that happens when the offline computer syncs the new data back into the central database?<br>\nI mean, if the keys are INTs, i\'ll have to renumber everything when importing basically. GUIDs will spare me of that.</p>\n'}, {'answer_id': 40279, 'author': 'vitule', 'author_id': 1287, 'author_profile': 'https://Stackoverflow.com/users/1287', 'pm_score': 0, 'selected': False, 'text': '<p>Using GUIDs saved us a lot of work when we had to merge two databases into one.</p>\n'}, {'answer_id': 40290, 'author': 'Eric Z Beard', 'author_id': 1219, 'author_profile': 'https://Stackoverflow.com/users/1219', 'pm_score': 0, 'selected': False, 'text': "<p>If your database is small enough to download to a laptop and work with it offline, you probably don't need to worry too much about the performance differences between ints and Guids.  But do not underestimate how useful ints are when developing and troubleshooting a system!  You will probably need to come up with some fairly complex import/synch logic regardless of whether or not you are using Guids, so they might not help as much as you think.</p>\n"}, {'answer_id': 40297, 'author': 'SQLMenace', 'author_id': 740, 'author_profile': 'https://Stackoverflow.com/users/740', 'pm_score': 1, 'selected': False, 'text': '<blockquote>\n  <p>Will access to these tables through\n  the GUID key be slow?</p>\n</blockquote>\n\n<p>There are other problems with GUIDs, you see GUIDs are not sequential, so inserts will be scattered all over the place, this causes page splits and index fragmentation</p>\n\n<p>In SQL Server 2005 MS introduced NEWSEQUENTIALID() to fix this, the only problem for you might be that you can only use NEWSEQUENTIALID as a default value in a table</p>\n'}, {'answer_id': 40303, 'author': 'Simon Munro', 'author_id': 3893, 'author_profile': 'https://Stackoverflow.com/users/3893', 'pm_score': 4, 'selected': True, 'text': "<p>Using Guids as primary keys is acceptable and is considered a fairly standard practice for the same reasons that you are considering them.  They can be overused which can make things a bit tedious to debug and manage, so try to keep them out of code tables and other reference data if at all possible.</p>\n\n<p>The thing that you have to concern yourself with is the human readable identifier.  Guids cannot be exchanged by people - can you imagine trying to confirm your order number over the phone if it is a guid?  So in an offline scenario you may still have to generate <strong>something</strong> - like a publisher (workstation/user) id and some sequence number, so the order number may be 123-5678 -.  </p>\n\n<p>However this may not satisfy business requirements of having a sequential number.  In fact regulatory requirements can be and influence - some regulations (SOX maybe) require that invoice numbers are sequential.  In such cases it may be neccessary to generate a sort of proforma number which is fixed up later when the systems synchronise.  You may land up with tables having OrderId (Guid), OrderNo (int), ProformaOrderNo (varchar) - some complexity may creep in.</p>\n\n<p>At least having guids as primary keys means that you don't have to do a whole lot of cascading updates when the sync does eventually happen - you simply update the human readable number.</p>\n"}, {'answer_id': 40429, 'author': 'Daniel Magliola', 'author_id': 3314, 'author_profile': 'https://Stackoverflow.com/users/3314', 'pm_score': 0, 'selected': False, 'text': '<p>@Simon,  </p>\n\n<p>You raise very good points. I was already thinking about the "temporary" "human-readable" numbers i\'d generate while offline, that i\'d recreate on sync. But i wanted to avoid doing with with foreign keys, etc.</p>\n'}, {'answer_id': 40445, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p>i would start to look at SQL Server Compact Edition for this! It helps with all of your issues.  </p>\n\n<p><a href="http://msdn.microsoft.com/en-us/library/bb380177.aspx" rel="nofollow noreferrer">Data Storage Architecture with SQL Server 2005 Compact Edition</a></p>\n\n<p>It specifically designed for </p>\n\n<blockquote>\n  <p>Field force applications (FFAs). FFAs\n  usually share one or more of the\n  following attributes</p>\n  \n  <p>They allow the user to perform their\n  job functions while disconnected from\n  the back-end network—on-site at a\n  client location, on the road, in an\n  airport, or from home. </p>\n  \n  <p>FFAs are usually designed for\n  occasional connectivity, meaning that\n  when users are running the client\n  application, they do not need to have\n  a network connection of any kind. FFAs\n  often involve multiple clients that\n  can concurrently access and use data\n  from the back-end database, both in a\n  connected and disconnected mode. </p>\n  \n  <p>FFAs must be able to replicate data\n  from the back-end database to the\n  client databases for offline support.\n  They also need to be able to replicate\n  modified, added, or deleted data\n  records from the client to the server\n  when the application is able to\n  connect to the network</p>\n</blockquote>\n'}, {'answer_id': 40459, 'author': 'chakrit', 'author_id': 3055, 'author_profile': 'https://Stackoverflow.com/users/3055', 'pm_score': 0, 'selected': False, 'text': '<p>First thought that comes to mind: Hasn\'t MS designed the DataSet and DataAdapter model to support scenarios like this?</p>\n\n<p>I believe I read that MS changed their ADO recordset model to the current DataSet model so it works great offline too. And there\'s also this <a href="http://msdn.microsoft.com/en-us/sync/bb887608.aspx" rel="nofollow noreferrer">Sync Services for ADO.NET</a></p>\n\n<p>I believe I have seen code that utilizes the DataSet model which also uses foreign keys and they still sync perfectly when using the DataAdapter. Havn\'t try out the Sync Services though but I think you might be able to benefit from that too.</p>\n\n<p>Hope this helps.</p>\n'}, {'answer_id': 41052, 'author': 'Portman', 'author_id': 1690, 'author_profile': 'https://Stackoverflow.com/users/1690', 'pm_score': 2, 'selected': False, 'text': '<p>@SqlMenace</p>\n\n<blockquote>\n  <blockquote>\n    <p>There are other problems with GUIDs, you see GUIDs are not sequential, so inserts will be scattered all over the place, this causes page splits and index fragmentation</p>\n  </blockquote>\n</blockquote>\n\n<p>Not true. <strong>Primary key != clustered index.</strong></p>\n\n<p>If the clustered index is another column ("inserted_on" springs to mind) then the inserts will be sequential and no page splits or excessive fragmentation will occur.</p>\n'}, {'answer_id': 41061, 'author': 'Portman', 'author_id': 1690, 'author_profile': 'https://Stackoverflow.com/users/1690', 'pm_score': 1, 'selected': False, 'text': '<p>You\'re correct that this is an old problem, and it has two canonical solutions:</p>\n\n<ul>\n<li><p>Use unique identifiers as the primary key. Note that if you\'re concerned about readability you can roll your own unique identifier instead of using a GUID. A unique identifier will use information about the date and the machine to generate a unique value. </p></li>\n<li><p>Use a composite key of \'Actor\' + identifier. Every user gets a numeric actor ID, and the keys of newly inserted rows use the actor ID as well as the next available identifier. So if two actors both insert a new row with ID "100", the primary key constraint will not be violated.</p></li>\n</ul>\n\n<p>Personally, I prefer the first approach, as I think composite keys are really tedious as foreign keys. I think the human readability complaint is overstated -- end-users shouldn\'t have to know anything about your keys, anyways! </p>\n'}, {'answer_id': 41065, 'author': 'SQLMenace', 'author_id': 740, 'author_profile': 'https://Stackoverflow.com/users/740', 'pm_score': 0, 'selected': False, 'text': "<p>@Portman  By default PK ==  Clustered Index, creating a primary key constraint will automatically create a clustered index, you need to specify non clustered if you don't want it clustered.</p>\n"}, {'answer_id': 41066, 'author': 'Matt Hinze', 'author_id': 2676, 'author_profile': 'https://Stackoverflow.com/users/2676', 'pm_score': 1, 'selected': False, 'text': '<p>Make sure to utilize guid.comb - takes care of the indexing stuff.  If you are dealing with performance issues after that then you will be, in short order, an expert on scaling.</p>\n\n<p>Another reason to use GUIDs is to enable database refactoring.  Say you decide to apply polymorphism or inheritance or whatever to your Customers entity.  You now want Customers and Employees to derive from Person and have them share a table.  Having really unique identifiers makes data migration simple.  There are no sequences or integer identity fields to fight with.</p>\n'}, {'answer_id': 1301231, 'author': 'Jeff Ferland', 'author_id': 90322, 'author_profile': 'https://Stackoverflow.com/users/90322', 'pm_score': 1, 'selected': False, 'text': '<p>I\'m just going to point you to <a href="https://stackoverflow.com/questions/170346/what-are-the-performance-improvement-of-sequential-guid-over-standard-guid/170363#170363">What are the performance improvement of Sequential Guid over standard Guid?</a>, which covers the GUID talk.</p>\n\n<p>For human readability, consider assigning machine IDs and then using sequential numbers from those machines as a possibility. This will require managing the assignment of machine IDs, though. Could be done in one or two columns.</p>\n\n<p>I\'m personally fond of the SGUID answer, though.</p>\n'}]
['https://Stackoverflow.com/questions/40230', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3314/']