0

Apologies in advance if this question has already been asked, but I really don't know how to search for the problem I have.

I'm developing a hotel booking and reservation management system with the intention of working with multiple clients, and it is expected that some clients will have different requirements, which include additional fields beyond the standard found in the booking response form (name, address, email, post code et cetera).

To accomplish this, I've created an "options" field that stores these additional fields and their attendant values as serialized data. However, the client wants those fields to be searchable. While this is possible, this quite clearly isn't the optimal way of storing data that needs to be searched.

Also, the table in question is of InnoDB format in MySQL.

About the only thing I can think of is moving these additional fields into a separate table, but that presents a lot challenges to the reading and writing process of bookings.

What is an optimal way of storing this kind of irregular data for the purposes of searching?

5
  • 2
    "but that presents a lot challenges to the reading and writing process of bookings."? A join? What are these "challenges"?
    – S.Lott
    Sep 27, 2011 at 11:02
  • Re-writing lots of code, and having the behaviour remain consistent across all clients, whether they're using extra fields or not. Sep 27, 2011 at 14:46
  • 1
    If "Re-writing lots of code" is part of your problem, then you waited too long to ask for design advice.
    – S.Lott
    Sep 27, 2011 at 15:14
  • The requirement for search came late, exceeding the original design specifications, thus me being here now. You'll find that context is everything, whereas opinion often amounts to very little. Sep 27, 2011 at 20:29
  • 1
    "context is everything". And -- seemingly -- omitted from the question. Here's the point. You have to redesign something. And if "rewriting lots of code" somehow makes a good design impossible, you're pretty well stuck, aren't you? There's no magical pixie dust that makes hard SQL redesign magically go away. Are we supposed to ignore the "a lot of challenges" complaint and suggest the correct rewrite? Or are we supposed to somehow magically avoid the necessary challenges of a rewrite?
    – S.Lott
    Sep 27, 2011 at 20:58

3 Answers 3

1

One option is to use a key-value table. Store the keys separately, e.g.:

CREATE TABLE keys (
  id INT(11) UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
  keyName VARCHAR(100)
);

CREATE TABLE values (
  key_id INT(11) UNSIGNED NOT NULL,
  customer_id INT(11) UNSIGNED NOT NULL, 
  value VARCHAR(100)
);

Definitely still not great, but it's a valid solution to your problem.

1
  • Yes, I like the idea. I think there's merit in that idea. Sep 27, 2011 at 14:51
0

Your problem isn't really a database problem but rather an issue of how to support multiple clients and their requirements in a single program.

The first thing you need to decide is whether multiple clients will be supported within a single instance of your program, or whether each client will have their own instance.

If each client has their own instance, then you can maintain the common codebase separately and customize each client's instance as required. It may take some planning to produce a system in which changes made to the common codebase are correctly inherited by the customized versions, but in the end each client will get exactly what they want.

If you're taking the multi-tenanted approach then you need to decide in advance the exact degree to which each client will be able to customize their system. You then provide for that customization in your database and application structure. At the simplest level, this allows each client to store their identifying information and a logo in a table someplace (possibly their own CSS link to really give the application a customized look).

In the case of "extra fields", this can be handled in several ways. One is to simply put 10 extra VARCHAR fields on the table in question, and allow each client to "name" these fields as they see fit (possibly with TYPE specifiers that your application will use to coerce the VARCHAR data if needed). However many of the columns they've named are shown (with the correct prompt) where appropriate in the user interface. This approach has the benefit that, once set up, you won't have any additional work to do per client.

Another approach would be to allow each client to have an additional 1 to 1 related table that stores the extra fields. In this case you can correctly name and type the fields in the database. The disadvantage is that to the extent you allow the client to freely choose what these fields are, you'll have to modify your user interface to "know" about each individual client.

2
  • "The first thing you need to decide is whether multiple clients will be supported within a single instance of your program, or whether each client will have their own instance." This is something I've discussed with the client, just in case the requirements of customers (the clients) needs exceed what the application is sensibly capable of supporting. Sep 27, 2011 at 14:48
  • Well proper version control can really help keeping these applications in sync. Pick an SCM that is great at merging
    – Evert
    Sep 27, 2011 at 15:41
0

I would probably separate it out in to a table of its own if it's something that you won't always be needing when reading that table, i.e., do you ever query this table without returning this field?

If you do choose to keep it in the same table there's no reason you can't place an index on it however which would speed up the searches as much as is possible without splitting up the data into logical chunks (which you can't if it can be "anything"). Do be warned that adding indexes will increase write times while decreasing read times (so don't do it in a system that will write more frequently than read or where the writes are more time sensitive).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.