views:

109

answers:

5

What is better extra query or extra column in database for data that will be available very less time.

Example: In Case of sub user management either i add one extra column super_user_id in main users table and make enrty if users types are sub_user and the default column value is -1 or i create new table and manage sub user in that table.

But in case of login i have to search in two tables and this i have to make one more query.

Thanks

A: 

Do you mean calculating a value in the your query versus storing a calculated value?

This depends on how often it will be updated, how big the data will be, how often it is needed. There may be no theoretical best answer, you will need to test and profile.

ck
A: 

Doing an additional query will always take more time.

Adding an extra column in DB will not have any significant impact, even if you should have thousands of rows.

Ergo, add extra column and save DB trafic :)

Steven
"Adding an extra column in DB will not have any significant impact" - can you back that statement up with some kind of evidence?
Dominic Rodger
The calculation *could* be done more efficiently in the front end.
ck
@Dominic: What will take the longest time. Doing one query or two queries? And unless he's storing huge amount of data in that extra column, it will not take any significant resources / space of the database. In theory doing two queries takes longer than one. In practice we would probably not notice since we are talking ms and not minutes.
Steven
In general, I agree. But how many extra columns is too many? If overdone, `select *` type queries pay the price, and DB traffic/performance *does suffer.
Paul McGuire
"Doing an additional query will always take more time". True. But it is also true that the maintenance of a derived column is a tax paid whenever we insert, update or delete from the table(s) involved. In systems which are primarily read-only (like many web-sites) the impact of that tax may well be negligible. In regular business OLTP systems the cost of that tax may well be exorbitant. We can't give meaningful advice without knowing more about the OP's specific system.
APC
@APC: I agree. My assumption for my answer, was that this is no big system and that the table does not contain to many columns. So in this case it doesn't matter much.
Steven
+1  A: 

There is no general answer; you'll have to be more specific. All I can provide are general principles.

All else being equal, you'll be better off with a well-normalized database without redundant information, for a number of reasons. But there are situations where redundant information could save your program a lot of time. One example is text formatted with Markdown: you need to store the original markup to allow for editing, but formatting the source every time you need the output may be extremely taxing on the system. Therefore, you might add a redundant column to store the formatted output and assume the additional responsibility of ensuring that that column is kept up-to-date.

All I know about your situation is that the postulated extra column would save a query. The only correct answer to that is that you should probably keep your table clean and minimal unless you know that the performance benefit of saving one query will make up for it. Remember, premature optimization is the root of all evil – you may find that your application runs more than fast enough anyways. If find while profiling that the extra query is a significant bottleneck, then you might consider adding the column.

Again, without more knowledge of your situation, it is impossible to provide a specific or concrete recommendation, but I hope that I've at least helped you to come to a decision.

Thom Smith
Thom is correct. You just have to weight the impact of having e.g. redundant data with how it will be further down the road. From personal experience, I went from about 10 tables to about 6, by having some redundant data. And it also saved me a lot of extra coding to get info from different tables and so on.
Steven
A: 

It depends on amount of redundency you will ad to table by adding a column. With proper indexing and design joins work better so no need to afraid of normalizing if required.

kedar
A: 

Use the second table. It will not require you to issue two queries. Instead, you will issue a single query JOINing the two tables together or, better yet, create a VIEW that does the JOIN for you:

SELECT usertable.col1, usertable.col2 superusertable.superuserid
    FROM usertable LEFT OUTER JOIN superusertable 
    ON usertable.userid = superusertable.userid

This allows you to maintain proper normalized structure, helps you in certain queries (like figuring out who is a super_user), and allows the database to optimize the search issues.

Larry Lustig
But in case of insert i have to check in both tables either this user name exist or not.
neverSayNo
1) That's a different question than the one you original posed, which was about querying and 2) No, if the user is not in the users table, they can't have a record in the other table. You can guarantee that's so by declaring the appropriate key relationships in the database design.
Larry Lustig