Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>crazy design, possibly the fake table names makes it stranger than it is.</p> <p>create indexes based on select usage.</p> <p>if you are searching on actual first names like "Joe" you need an index on FirstNames.Name</p> <p>if you are searching on first name ids like 123, you have an index: FirstNames.FirstNameId</p> <p>if you want to search on FirstNames.name and/or MiddleNames.name and/or Surnames.name you need to have indexes on the combinations that you will use, and the more you make, the harder for the query to pick the best one.</p> <p>ditch the view and write a dedicated query for the purpose:</p> <p>go after first/middle</p> <pre><code>select FirstNames.name ,MiddleNames.name ,Surnames.name FROM FirstNames INNER JOIN MiddleNames ON FirstNames.FirstNameId=MiddleNames.FirstNameId INNER JOIN Surnames ON FirstNames.FirstNameId=Surnames.FirstNameId WHERE FirstNames.Name='John' AND MiddleNames.Name='Q' </code></pre> <p>go after last</p> <pre><code>select FirstNames.name ,MiddleNames.name ,Surnames.name FROM Surnames INNER JOIN FirstNames ON Surnames.FirstNameId =FirstNames.FirstNameId INNER JOIN MiddleNames ON FirstNames.FirstNameId=MiddleNames.FirstNameId WHERE Surnames.Name='Public' </code></pre> <p>just make sure you have indexes to cover your main table in the "where" clause</p> <p>use SET SHOWPLAN_ALL ON to make sure you are using an index ("scans" are bad "seeks" are good")</p> <p><strong>EDIT</strong><br> if possible break the names apart before searching for them:</p> <pre><code>exec uspSearchForPeople 'joe',null,'blogs' (1 result) exec uspSearchForPeople 'joe',null,null (1 result) exec uspSearchForPeople null,null,'blogs' (2 results) exec uspSearchForPeople 'jon',null,'skeet' (1 result) exec uspSearchForPeople null,null,'skeet' (1 result) </code></pre> <p>within the stored procedure, have three queries:</p> <pre><code>if @GivenFirstName is not null --search from FirstNames where FirstNames.name=@value &amp; join in other tables else if @GivenMiddleName is not null --search from MiddleNames where MiddleNames.name=@value &amp; join in other tables else if @GivenLastName is not null --search from Surnames where Surnames.name=@value &amp; join in other tables else --error no names given </code></pre> <p>have an index on all three tables for Names.</p> <p>if you can not split the names apart, I think you are out of luck and you will have to table scan every row in each table.</p> <p>Just think of a phone book if you don't use the index and you are looking for a name, you will need to read the entire book</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload