exam questions

Exam 70-762 All Questions

View all questions & answers for the 70-762 exam

Exam 70-762 topic 1 question 70 discussion

Actual exam question from Microsoft's 70-762
Question #: 70
Topic #: 1
[All 70-762 Questions]

Note: This question is part of a series of questions that present the same scenario. Each question in the series contains a unique solution. Determine whether the solution meets the stated goals.
The Account table was created by using the following Transact-SQL statement:

There are more than 1 billion records in the Account table. The Account Number column uniquely identifies each account. The ProductCode column has 100 different values. The values are evenly distributed in the table. Table statistics are refreshed and up to date.
You frequently run the following Transact-SQL SELECT statements:

You must avoid table scans when you run the queries.
You need to create one or more indexes for the table.
Solution: You run the following Transact-SQL statement:
CREATE NONCLUSTERED INDEX IX_Account_ProductCode ON Account(ProductCode);
Does the solution meet the goal?

  • A. Yes
  • B. No
Show Suggested Answer Hide Answer
Suggested Answer: A 🗳️
References:
https://msdn.microsoft.com/en-za/library/ms189280.aspx

Comments

Chosen Answer:
This is a voting comment (?). It is better to Upvote an existing comment if you don't have anything to add.
Switch to a voting comment New
New_user
Highly Voted 5 years, 4 months ago
Answer is incorrect. The second statement leads to table scan when searching AccountNumber
upvoted 14 times
Bartek
5 years, 3 months ago
and balance, to get index seek we should add : CREATE NONCLUSTERED INDEX IDX ON dbo.<tableName> (PRODUCTCODE) INCLUDE (ACCOUNTNUMBER,BALANCE)
upvoted 5 times
JohnFan
5 years, 2 months ago
Why do you include ACCOUNTNUMBER, instead of create clustered Index for ACCOUNTNUMBER?
upvoted 2 times
JohnFan
5 years, 2 months ago
Wow.. Maybe this works.. Basically you create a non-clustered index on Heap.. So the other side is RID lookup (heap), which is not scan.. and they are combined by Nested Loops.. https://www.sqlservercentral.com/blogs/sql-server-part-5-explaining-non-clustered-index-on-heap
upvoted 1 times
JawwadAK
5 years, 2 months ago
But again on very next statement table will scan :-) so it's wrong answer here.
upvoted 1 times
...
...
...
...
MML
4 years, 10 months ago
I agree
upvoted 1 times
...
...
eggzamtaker
Most Recent 4 years, 4 months ago
table scan is NOT THE SAME THING AS AN index scan. get it through your head people. it does not matter if the "scan" is occurring on a clustered index. the question is specifically talking about a table scan.
upvoted 1 times
...
TheDUdeu
4 years, 4 months ago
Why do people think this will table scan? Both statements will not table scan it is an index on the where.
upvoted 1 times
...
TheDUdeu
4 years, 5 months ago
I can see the first one doing a full scan but how does the second statement give you a full scan? You have a nonclustered index on product code and you filter on the product code. I will look at this tomorrow.
upvoted 2 times
Alex5x
4 years, 5 months ago
I can explain it. Since there are more than 1 billion records in the Account table and only 100 different product codes, for each product code you get 1,000,000,000/100 = 10,000,000 in average. So, for ProductCode = 'CD', you should make around 10 millions RID lookups to get AccountNumber and Balance if you seek IX_Account_ProductCode index. It is not efficient, so optimizer preferes to scan the table. If you created the covering index: CREATE NONCLUSTERED INDEX IX_Account_ProductCode ON Account(ProductCode) INCLUDE (AccountNumber, Balance), then it would be absolulty different story with a happy ending:)
upvoted 4 times
...
Hoglet
4 years, 5 months ago
One thing to watch for when looking at query plans is the Seek with a Scan. If we were to create a clustered index, such as create clustered index idx_account_productcode on dbo.account(productcode); And query ProductCode = 'CD', then the plan will show a "Seek". But this is actually a Seek to the 1 index entry where ProductCode = 'CD', and then a scan of the index to the last entry. If you have a Seek which is expected to return multiple rows and it not part of nested loop, its a Seek and a Scan
upvoted 1 times
...
...
Alex5x
4 years, 6 months ago
Here is the SQL for the test: CREATE TABLE dbo.Account ( AccountNumber int not null, ProductCode char(2) not null, Status tinyint not null, OpenDate date not null, CloseDate date, Balance decimal(15,2), AvailableBalance decimal(15,2) ); GO INSERT INTO dbo.Account(AccountNumber,ProductCode,Status,OpenDate,Balance) values (111101,'AB',1,GETDATE(),£200.50), (111102,'CD',1,GETDATE(),£300.50), (111103,'EF',1,GETDATE(),£500.50), (111104,'GH',0,GETDATE(),£100.50), (111104,'IJ',0,GETDATE(),£100.50), (111104,'KL',0,GETDATE(),£100.50), (111104,'MN',0,GETDATE(),£100.50), (111104,'OP',0,GETDATE(),£800.50), (111104,'QR',0,GETDATE(),£600.50), (111104,'ST',0,GETDATE(),£100.50); GO 10000 In total, there will be 100 000 records in the table. Both queries give you Table scan. Add the index: CREATE NONCLUSTERED INDEX IX_Account_ProductCode ON Account(ProductCode); Both queries still give you Table scan. So the answer is NO.
upvoted 1 times
...
Luzix
4 years, 6 months ago
It´s yes for the first query and no for the second. I have checked it in sql server. FIRST QUERY PRODUCTS: SELECT <-- COMPUTER SCALAR <-- STREAM AGGREGATE <-- NESTED LOOPS <-- INDEX SEEK & RID LOOKUP SECOND QUERY: SELECT <-- TABLE SCAN
upvoted 2 times
Hoglet
4 years, 5 months ago
It's a 1billion row table, SQL Server will not do a nested loop and RID Lookups to return 90% of a 1billion row table.
upvoted 2 times
...
...
ZSQL
4 years, 6 months ago
create table dbo.account ( accountnumber int not null, productcode char(2) not null, status tinyint not null, opendate date not null, closedate date null, balance decimal(15,2), availablebalance decimal(15,2) ); insert into dbo.account(accountnumber,productcode,status,opendate,balance) values(111101,'AB',1,GETDATE(),£200.50), (111102,'Ac',1,GETDATE(),£300.50), (111103,'CB',1,GETDATE(),£500.50), (111104,'FB',0,GETDATE(),£100.50); create nonclustered index idx_account_productcode on dbo.account(productcode); select productcode,sum(balance) as TotalSUM from dbo.account where productcode<>'FB' group by productcode; select accountnumber,balance from dbo.account where productcode='FB'; actual execution plan: tested answer is wrong: 1st query will be INDEX SEEK 2nd query will be INDEX SCAN unless balance col include with above ncl index
upvoted 1 times
ZSQL
4 years, 6 months ago
sorry 2nd query will be TABLE SCAN unless balance col and accountnumber col include with above ncl index
upvoted 1 times
...
Hoglet
4 years, 5 months ago
Your 1st query example is incorrect because you are not using representative data. We have a billion row table, and 10 million rows per product code. SQL Server is going never going to do 900 million RID lookups on a 1billion row table, it will scan the table instead. There are rules of thumb regarding tipping points and cardinality, and they should be ignored. But 90% of a table / index will always be a scan of the object
upvoted 1 times
...
...
giuPigna
4 years, 6 months ago
Answer is YES. With a NCI on ProductCode, you will have only index seeks (for ProductCode) and RID lookup (for select AccoundNumber and Balance). However the best solution is NIC on ProductCode INCLUDE Balance and a CI on AccountNumber.
upvoted 2 times
Luzix
4 years, 6 months ago
It´s yes for the first query and no for the second. I have checked it in sql server.
upvoted 1 times
...
giuPigna
4 years, 5 months ago
Tried in SqlServer with few records and had Table Scan. Tried to add a lot of records with unique account number and a couple of different product code and had index seek on second query. I'm not sure if the query optimizer prefers to use index seek/rid lookup when the table has a lot of records or not. In my opinion, not considering what we tried in sql server, in the NCI i should find a list of row ids, so I should find a RID lookup on the select to retrieve missing columns. Correct me if i'm wrong.
upvoted 1 times
...
...
SoupDJ
4 years, 6 months ago
Question for the group - why do you think an inequality (<>) in a query dictates that an index scan will always be used? My understanding of when a scan is used is when there is no index that will allow the optimizer to reduce its workload - and this index should I believe. In addition, there is no requirement that seeks can only use clustered indexes - although I'm sure its much more common.
upvoted 1 times
...
Oooo
4 years, 7 months ago
Answer is no. Tested it. Create a table add few records and test it yourself. :)
upvoted 1 times
databasejamdown
4 years, 7 months ago
What I find interesting is that the first query will always lead to a full scan of either a table or an index. The <> operator will guarantee that. The only way to improve it is a filtered index. However, if the a non filtered index ends up having less data pages than the the table, then an index scan will be faster than a table scan
upvoted 1 times
...
...
stm22
4 years, 10 months ago
this is same question as #62, but the given answers are conflicting
upvoted 4 times
geekeek1
4 years, 4 months ago
Exactly
upvoted 1 times
...
...
stm22
4 years, 10 months ago
part 2: our goal: You must avoid table scans when you run the queries. An index scan or table scan is when SQL Server has to scan the data or index pages to find the appropriate records. A scan is the opposite of a seek, where a seek uses the index to pinpoint the records that are needed to satisfy the query. the given command "CREATE NONCLUSTERED INDEX IX_Account_ProductCode ON Account(ProductCode);" will allow optimizer to perform seek (not scan) on queries " where productcode ..." thus answer is Yes
upvoted 4 times
stm22
4 years, 10 months ago
i now think i was wrong...it will have to scan table for balance answer is NO sorry to mislead you
upvoted 4 times
CharlieBrownIsGreat
4 years, 5 months ago
It will do a *** lookup *** for the balance. Not the same thing as a table scan.
upvoted 2 times
...
...
...
stm22
4 years, 10 months ago
part 1: what the clues are telling us: the CREATE TABLE has no primary key = no clustered index 1 billion rows = needs some indexes The Account Number column uniquely identifies each account. = every row has unique Account Number , but it's not defined as primary key The ProductCode column has 100 different values/The values are evenly distributed in the table. = each ProductCode covers about 10 million rows Table statistics are refreshed and up to date. = " In some cases you can improve the query plan and therefore improve query performance by updating statistics" from https://docs.microsoft.com/en-us/sql/relational-databases/statistics/statistics?view=sql-server-ver15 both select queries where clause looks for productcode
upvoted 2 times
...
mrn0107
4 years, 11 months ago
Wrong answer. If it was a cluster index then would be ok.
upvoted 3 times
Hoglet
4 years, 5 months ago
Except that the Clustered Index would be the table and you would be scanning it So the answer is No
upvoted 1 times
...
...
Community vote distribution
A (35%)
C (25%)
B (20%)
Other
Most Voted
A voting comment increases the vote count for the chosen answer by one.

Upvoting a comment with a selected answer will also increase the vote count towards that answer by one. So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.

SaveCancel
Loading ...