Jump to content

Recommended Posts

I'm quite the database intermediate, which could be a dangerous thing (!) so I thought I'd ask some questions here first.

 

I've got a range of tables in my application, and for most of my queries I need to do one or more JOINs. I have one particular query which will JOIN across 5 tables, and due to the way I'm about to allow all users to now belong to more than one group, I have to add a 6th (relationship) table to this (and most other) queries.

 

The thing is, quite a few of the multiple JOIN series could be omitted if I just added a kind of "shortcut" column (that effectively give me the same information as the JOINs) to one of the right-hand tables, and I'd be able to do quite simple queries. Once new entries have been inserted into the table the data in this new entry will never change, so, my question is, now that all the queries are becoming quite complex, how to optimize both the code and the performance?

 

  • Do I stick with JOINs and retain full normalisation?
  • Do I start using a VIEW (I'm new to views)?
  • Or do I add this new column and just start querying one or possibly two tables max rather than a hefty series of JOINs each time?

 

I realise that there will be a minute level of redundancy with the last method, but it it worth it at the expense of cheaper (I presume) DB calls?

 

At the moment the tables have a few hundred rows, but ultimately I expect them to have a few hundred thousand.

 

Your thoughts would be really valuable, thanks.

 

Cheers,

Dave

 

Well, I have a hierarchy of tables something like this:

 

users

users_companies

companies 
services 
invoices 
items 

 

So say I query something like this:

 

get all items for all unpaid invoices for company x (in the past 6 months) for user y

 

That makes quite a few joins as everything is perfectly normalised.

 

But I could include a reference to the company in each invoice, or even item table, which would save me deriving the company by JOINing up the chain to :

 

services
companies 
users_companies

 

(users_companies is a relationship table as users can potentially belong to many companies).

 

Does that make sense?

I could use a stored function, but that won't actually affect the efficiency of the call will it? It will just package the call into something accessible.

 

I was more interested in the actual performance benefits of one method over the other

I wouldn't de-normalize until you start to see serious performance issues -- and there are all sorts of ways around that too without messing with the underlying tables (caching and such).

I agree with you. De normalizing can cause problems when updating.

I'm using views only for reporting.

For showing individual or few rows I prefer select from master table then select for each child table instead of using JOINS. That is not cumbersome when using DAO.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.