sql server - SQL view for searching is slow -


we trying build view provides data in flattened manner backs simple search.

the challenge has been while displaying fields name, needing search across fields address don't have displayed, can still returned. address stored in address table, , there can many addresses each client record.

so thought view put main client record various addresses. resulted in duplicate rows because of join.

next, wrote query rolls these child tables , concatenates them together. makes easy search against , doesn't end duplicates. put view, it's pretty slow. i'm preferring keep view because of nature of component accessing data, however, i'm not opposed it.

can sped or going wrong way?

here's sql (t-sql, actually):

with clientbase (     select distinct client.clientid, clientnames.firstname, clientnames.lastname, clientnames.middlename,          addresshistory.address1 + ' ' + addresshistory.address2 streetaddress,          addresshistory.city, contactinfo.contact phone     client         inner join clientnames on clientnames.clientid = client.clientid         inner join addresshistory on addresshistory.clientid = client.clientid         inner join contactinfo on contactinfo.clientid = client.clientid ) select distinct vcs.clientid, vcs.firstname, vcs.lastname, vcs.middlename,                 (select stuff ((select distinct top (100) percent ';' + clientbase.streetaddress [text()]                                 clientbase                                 clientbase.clientid = vcs.clientid                                 xml path('')), 1, 1, '') streetaddress) streetaddress,                 (select stuff ((select distinct top (100) percent ';' + clientbase.city [text()]                                 clientbase                                 clientbase.clientid = vcs.clientid                                 xml path('')), 1, 1, '') city) city,                 (select stuff ((select distinct top (100) percent ';' + clientbase.phone [text()]                                 clientbase                                 clientbase.clientid = vcs.clientid                                 xml path('')), 1, 1, '') phone) phone clientbase vcs 

create indexed view , try avoid unnecessary distinct clause. create indexed views


Comments

Popular posts from this blog

c++ - CryptStringToBinary API behavior -

c++ - Correct method for redrawing a layered window -

java.util.scanner - How to read and add only numbers to array from a text file -