Why does this sql query do a key lookup?

14,003

Solution 1

Because it is selecting *.

It uses the non clustered index to locate the row(s) but then needs to go and fetch the data to return.

To avoid the bookmark lookup you would need to make the non clustered index a covering index (ideally by reducing the number of columns in the select list but possible also by adding new columns into the index itself or as included columns)

If you have a clustered index on the table the row locator in the non clustered index will include the clustered index key so it won't need a bookmark lookup to satisfy queries on just the AccountIdentifier and clustered index columns.

Solution 2

Key lookup doesn't mean "look up the key", but "look up the row based on the key".

Solution 3

See these articles and blog posts for some more background info on key lookups / bookmark lookups:

Share:
14,003
BC.
Author by

BC.

-----BEGIN PGP PUBLIC KEY BLOCK----- Version: PGP Key Server 0.9.6 mQGiBEnb18kRBACvY4O/sCOkTIpEZgqi+Wx0jaODDSEm01w5HxVxTaoqPKShavXn HqtxFEXAuYRy0THcFsaoS1lbgOyoWyZyPMSFDl1c9ul+Ub49W1FJwE5IfMJMMdPH gQo5aJzGvWhE31yAQZmN1hZLPKXC9NAcbDPLYCEkDkKINAtLEJqOZDXBqwCg2/bZ xhlwJOvl0/mq1hE+0l7dhvcD/jBqSYHiPez2iNQGON1/LGGzpk8X2jRqhOWcUfUI OdS+vWtFtTJWL0/lm17D0vfqE2iuUXdXuzD+PJDTD24JZkVp7G30QSbq+octYtfh mdAwgKG2zBoZyi50EE1qSHd9P4BEm/sUWbD+1FYF9mOlfd0RBN38s/kixza3A5yE pE3LA/9Gl/+7grlVhffeHkmeMqQYofAr9awx/VfgwmfPQl0rV9Cfdca9+wVtRCRS NN5kBmJlsn0L90K8RJbZK1QbzFwrn64+uFvtnaFqLiemKZreZmJ4v/bIJn+ft3Lp AGTWXYRX3iROvnOwL2HOXFhIwXJC5tOjy654B9+0fTpiAxKfj7QnQnJhbmRvbiBD cm9mdCA8YnJhbmRvbi5jcm9mdEBnbWFpbC5jb20+iGAEExECACAFAknb18kCGwMG CwkIBwMCBBUCCAMEFgIDAQIeAQIXgAAKCRDNixZyJF6nYa+sAJ9ZFDJDSUmGtbFG glhTR/LwI9CG5gCgh2b1cuYe1lDHTLfr+XQu+RatCbS5Ag0ESdvXyRAIAI0LKksn X8PisiI9tPcbs6SHJKM5UFkT3hiwNXvh7tSXXfBp6+DBXCzwVQh1ADwh7yw4OZC5 mzKTX+F9csBJ/qU5d977y7LWLHrr13m4c77NnqV97FsO/wK9K4I4UNwRFUbDXSwr 02tUGV+FEbDJ4P94k3zPgLiaQUZ3YLQEGbPWJgCCWrmproRUzcpcBXcujMWN0TPU sXlsBPR1eNi21jy/nnxxicifa1uOWTZSSWpUgxLZ4D5asBy4TVySmhWISnREntH3 npPHoeEkcUDjjRmD5SgzO4guMHW40A1yW2l8jqBuLlhUQVW60uPVqwaHzP4aaLNR Flg4A25GCrbYMOMAAwYH/3jJfOhRoctoFE39q/t4M8bxOkwz7Os2IsMpIWiJuveq MW6ePBzY3KMGZ0FhN89iRpo6SkBixkyLJsshYv05qvBjZQ/uUbVK5CYDRGzuMt1Q K+Xy6JD/cdnUB0r/Ct8suRTI+Bw1Alezf5dptpgYAP3k8Ai6jtBni/xgioG4s/T+ RbxR6T81NFdm477qEm11R1Ob4Gs7eeaHWLZspOEkx+pwXJ/sxNE3OZJ7ze+7hPZH ArnI5w/hsNIZZfGygnVevhmti8eEwSkJwO9DAsZR4Fhge8f2CQS+zWUUsbxS63T3 y2zQdUvpORG/pf0lvZRQaG66kDxBL6qHBC3ELXBzmvaISQQYEQIACQUCSdvXyQIb DAAKCRDNixZyJF6nYZviAJ0ZMI5zpoS2DAQHMI0kOpDdF85i1ACeJs7T5QlkxLXx eYsfmA6k/16yDoI= =tTpB -----END PGP PUBLIC KEY BLOCK-----

Updated on June 18, 2022

Comments

  • BC.
    BC. about 2 years

    I have a table User with a bunch of indexes. One of them is a unique index on the AccountIdentifier column.

    Since this is a unique index, why is a key lookup required in addition to the index seek? The index seek tooltip reports that only one record is returned. I've also tried converting the index to a "unique key" type.

    alt text http://s3.amazonaws.com/brandonc.baconfile.com/pitchurs/tmp/capture_2.png