Could you point a way to split a very long MSSQLServer query to make it retrieves just parts of the lines for each time?

Hi Experts

Could you point a way to split a very long MSSQLServer query to make it retrieves just parts of the lines for each time?

I'm running a query that must retrieve 3.200.0000 lines, resulted from some group operations:

     SELECT nome, Nome_Valido, cpf, CPF_Valido, data_nasc, telefone, endereco, cidade, uf, cep, 
       max(Cem) AS  cem , max(Infosaude) AS  infosaude, max(Procare) as procare
FROM v_UnificacaoPacientes_v5
GROUP BY nome, Nome_Valido, cpf, CPF_Valido, data_nasc, telefone, endereco, cidade, uf, cep
ORDER BY nome;

Open in new window


Is that a way to partitionate the results in subsets of lines and then assemble it ?

Thanks in advance.
Eduardo FuerteDeveloper and AnalystAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

pcelbaCommented:
Did you try OFFSET and FETCH NEXT clauses?
     SELECT nome, Nome_Valido, cpf, CPF_Valido, data_nasc, telefone, endereco, cidade, uf, cep, 
       max(Cem) AS  cem , max(Infosaude) AS  infosaude, max(Procare) as procare
FROM v_UnificacaoPacientes_v5
GROUP BY nome, Nome_Valido, cpf, CPF_Valido, data_nasc, telefone, endereco, cidade, uf, cep
ORDER BY nome
OFFSET 100000 ROWS
FETCH NEXT 100000 ROWS ONLY;

Open in new window

You may use variables or even expresions for OFFSET and FETCH NEXT.
More info https://docs.microsoft.com/en-us/sql/t-sql/queries/select-order-by-clause-transact-sql
OTOH, you should answer the question why do you need to retrieve 3 mio rows? Who will read it? Shouldn't you consolidate them on the SQL Server?

One more thing: It seems your query is from some view. OFFSET and FETCH are not supported in indexed views or in a view that is defined by using the CHECK OPTION clause. You should also read all other notes on OFFSET and FETCH in the above link.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Eduardo FuerteDeveloper and AnalystAuthor Commented:
Hi Pcelba

We need to migrate all the medical patients from legate system to the new CRM system.
Eduardo FuerteDeveloper and AnalystAuthor Commented:
By going the strategy you pointed, I'm planning to split in 04 parts

     SELECT nome, Nome_Valido, cpf, CPF_Valido, data_nasc, telefone, endereco, cidade, uf, cep, 
       max(Cem) AS  cem , max(Infosaude) AS  infosaude, max(Procare) as procare
FROM v_UnificacaoPacientes_v5
GROUP BY nome, Nome_Valido, cpf, CPF_Valido, data_nasc, telefone, endereco, cidade, uf, cep
ORDER BY nome
OFFSET 10000000
FETCH NEXT 1000000 ROWS ONLY;



     SELECT nome, Nome_Valido, cpf, CPF_Valido, data_nasc, telefone, endereco, cidade, uf, cep, 
       max(Cem) AS  cem , max(Infosaude) AS  infosaude, max(Procare) as procare
FROM v_UnificacaoPacientes_v5
GROUP BY nome, Nome_Valido, cpf, CPF_Valido, data_nasc, telefone, endereco, cidade, uf, cep
ORDER BY nome
OFFSET 10000001
FETCH NEXT 1000000 ROWS ONLY;

SELECT nome, Nome_Valido, cpf, CPF_Valido, data_nasc, telefone, endereco, cidade, uf, cep, 
       max(Cem) AS  cem , max(Infosaude) AS  infosaude, max(Procare) as procare
FROM v_UnificacaoPacientes_v5
GROUP BY nome, Nome_Valido, cpf, CPF_Valido, data_nasc, telefone, endereco, cidade, uf, cep
ORDER BY nome
OFFSET 20000001
FETCH NEXT 1000000 ROWS ONLY;

SELECT nome, Nome_Valido, cpf, CPF_Valido, data_nasc, telefone, endereco, cidade, uf, cep, 
       max(Cem) AS  cem , max(Infosaude) AS  infosaude, max(Procare) as procare
FROM v_UnificacaoPacientes_v5
GROUP BY nome, Nome_Valido, cpf, CPF_Valido, data_nasc, telefone, endereco, cidade, uf, cep
ORDER BY nome
OFFSET 30000001
FETCH NEXT 1000000 ROWS ONLY;

Open in new window


Am I in the right way?
Price Your IT Services for Profit

Managed service contracts are great - when they're making you money. Yes, you’re getting paid monthly, but is it actually profitable? Learn to calculate your hourly overhead burden so you can master your IT services pricing strategy.

Eduardo FuerteDeveloper and AnalystAuthor Commented:
Could you check what's wrong when running this:

img001
Eduardo FuerteDeveloper and AnalystAuthor Commented:
Just considering

v_UnificacaoPacientes_v5

Open in new window


Is a view...
Eduardo FuerteDeveloper and AnalystAuthor Commented:
Only missed the word ROWS

OFFSET 100000 ROWS

Open in new window

pcelbaCommented:
Yes the ROWS word was missing in my original post and I've fixed it later.
If you are planning just 4 parts then the concept is OK. The OFFSET in the first query should be 0 and all other offsets should be minus one - 1000000, 2000000, ...
Eduardo FuerteDeveloper and AnalystAuthor Commented:
Thank you.

Unfortunatelly the performance is very slow in my case, so I have to adopt another strategy.
pcelbaCommented:
Slow performance means missing index on the nome column. Did you look at execution plan? Of course, I don't know the view definition and 3 mio rows from a (possibly) complex view does not help to improve the performance...

Just remember the data porting to a new system is one time task and if it takes hours then it is still OK.
Eduardo FuerteDeveloper and AnalystAuthor Commented:
I had tried to recreate the view as an indexed view on the column name but the specific database doesn't allow that.
I agree with your considerations about data migration is an unique event that where time to be consumed on strategies must to be only the necessary.

What I did is to split the data using the name initials and save the results on .CSV(s) - and then maybe join all the CSV(s).

Another company of the group I found have at about 21 million register. By going this way is slow but possible and I'm getting it.
pcelbaCommented:
OFFSET and FETCH does not work on indexed views... and CSV can contain characters which are not processed correctly...

If you are porting data between two different servers then you could have two more options:
1) Create a backup, restore it on the second server and then all queries should work relatively quickly. You may even prepare "export database" on the original server and then port the backup of this db.
2) Create a Linked server on the new machine and execute necessary queries on the linked server. Of course, millions of data rows will still consume time.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.