in Education by
If I have a view in SQL which contains various computed columns, some of which could be very expensive, but only select a few columns out at any one time will I be taking any more of a performance hit, than if I was to separate the views out to multiple views and use them individually? For example if I have 5 columns in my table, and my view contains those same 5 columns, but also 10 simple computed columns and 10 expensive computed columns (using averages or similar) and decide to only select out one or two of the simple computed columns, would I expect that to be any more expensive than if I had separated the expensive columns into their own view? Edit: I'm specifically interested in answers regarding SQL Server and Postgres databases, but a more general response if appropriate will suffice. Edit2: I've looked at query plans in SQL Server and it appears to not bother making a plan for the computed columns when they aren't selected, so I'm hopeful that it's fine to combine all the columns into one view, but would like confirmation :D Edit 3: @NaveenBabu I don't have any performance problems yet - this is somewhat hypothetical. The extra columns will mostly be things like: DATEPART(mm, aDateField), DATEPART(dd, aDateField) ie. simple cheap extensions to the table. But there will be more complicated expensive columns like: (SELECT COUNT(*) FROM events WHERE events.iTicket = tickets.iCode) as NumberOfEvents So I guess if you want a generic example the view would be: CREATE VIEW TicketsView AS SELECT tickets.idx, tickets.a, tickets.b, tickets.c, tickets.d, DATEPART(mm, a) as ticketMonth, DATEPART(dd, a) as ticketDay, DATEPART(yy, a) as ticketYear, (SELECT COUNT(*) FROM events WHERE events.iTicket = tickets.idx) as numEvents FROM tickets Or something like that. The last column is clearly more expensive than the others so: If I SELECT tickets.idx, tickets.b, tickets.ticketMonth FROM TicketsView will it need to do the subselect / count to calculate numEvents, as I haven't selected it out from the view? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
In SQL Server the basic principle is that Views are expanded in-line. They're like code-templates that get copied and pasted into your own query. There are other over-heads as well, and you can specify a view not be be expanded in this way, but it's a good general description. One thing that this means is that fields NOT referenced in your query don't get copied though. If a join is needed to derive that column, the join is still necessary - It could duplicate or filter rows from another table, etc - But the scalar calculations would most likely not happen. In your example, using a correlated sub-query for the last field is often slower than a join alternative. But in your case this has a benefit - If you don't select that field, the correlated-sub-query isn't happening. You're introducing a cost when it is selected, and a saving when it's not.

Related questions

0 votes
    Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    I am trying to consume this API for dart using Flutter: https://pixcut.wondershare.com/api.html. My ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 6, 2022 in Education by JackTerrance
0 votes
    Match column I with column II and select the correct answer using the codes given below the columns. Column - I Political ... 2, D - 4 Select the correct answer from above options...
asked Aug 3, 2022 in Education by JackTerrance
0 votes
    I would like to ask for performance of SQL. This is my query: Select A.code ,A.name ,(Select B. ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
0 votes
    How do I perform an IF...THEN in an SQL SELECT statement? For example: SELECT IF(Obsolete = 'N' OR ... Saleable, * FROM Product Select the correct answer from above options...
asked Jan 28, 2022 in Education by JackTerrance
0 votes
    This is my code it's just starting the scan but it is not completing ,where is the error in it. ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 26, 2022 in Education by JackTerrance
0 votes
    i am in hurry , i want to sleep early; but i have a test for tomorrow, i have not study what can ido Select the correct answer from above options...
asked Dec 29, 2021 in Education by JackTerrance
0 votes
    Which of the following return a subset of the columns of a data frame? (a) select (b) retrieve (c) ... and Operations of R Programming Select the correct answer from above options...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    I have some website which requires a logon and shows sensitive information. The person goes to the page, is ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 24, 2022 in Education by JackTerrance
0 votes
    I have two insert statements, almost exactly the same, which run in two different schemas on the same ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 21, 2022 in Education by JackTerrance
0 votes
    __________________ are those search engines that are intended and designed not to take our searched data or browsing habits ... mode 3) In-private mode 4) Private search engines...
asked Dec 29, 2020 in Technology by JackTerrance
0 votes
    ________ function is similar to the existing subset() function in R but is quite a bit faster. (a) ... Operations of R Programming Select the correct answer from above options...
asked Feb 12, 2022 in Education by JackTerrance
0 votes
    I am trying to figure out how to pull only specific rows and columns using Google apps script. The ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 13, 2022 in Education by JackTerrance
0 votes
    Is there an 'official' way to get all input elements of a form including those outside of the form ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    The ______ clause allows us to select only those rows in the result relation of the ____ clause that ... , Database Interview Questions and Answers for Freshers and Experience...
asked Oct 11, 2021 in Education by JackTerrance
...