Wednesday, 14 August 2013

Should I create View or write Query instead for better performance?

Should I create View or write Query instead for better performance?

To help you understand, think like, I am working on some email system
where email are saved in Db and can or can't be Draft.
Problem:
I have 2 tables with following structure
tblUsers
=========
Id - PK
Fname
..
tblMails
=========
Id - PK
UserId - F.K. tblUsers.Id
isDraft - Bit(boolean) field, default = 0
I need to search all mails and drafts in 'tblMails' for particular UserId
saperately. To accomplish this, i have two ways:
I can create 2 View - 'showMails' and 'showDraft' that select all records
from 'tblMails' on the basis of 'isDraft' field. Thereafter, I can apply
'WHERE' condition on the View to fetch details for particular user.
Instead of writing View, I apply 'isDraft=1' or 'isDraft=0' everywhere in
my code.
Which of these should be more appropriate considering computational cycles
required to fetch the records?

No comments:

Post a Comment