Your SQL needs lots of correction.
Let's first dissect your SQL a bit:
FROM RealTimeVending.dbo.rtv_turnover_transaction as t,
RealTimeVending.dbo.rtv_trans_articles as ta,
RealTimeVending.dbo.articles as art, RealTimeVending.dbo.groups,
RealTimeVending.dbo.Clients as s,
RealTimeVending].dbo.rtv_transactions as tr,
RealTimeVending.dbo.tills as till, RealTimeVending.dbo.Ubicacion as u,
RealTimeVending.dbo.Operadores as o
where t.operador_id=o.ID and t.transaction_id=ta.transaction_id and
art.id=ta.article_id and s.id=t.cliente_id and tr.id=t.transaction_id
and groups.id=art.group_a_id and t.ubicacio_id=u.id
and convert(date,t.trans_date) >='"+globalMap.get("datainici")+"'and
convert(date,t.trans_date) <= '"+globalMap.get("datafinal")+"'and
and (s.codigo IS NULL or s.codigo like '%"+globalMap.get("client")+"%')
and (t.total_amount IS NULL or t.total_amount like'"+globalMap.get("amount")+"%')
You are using old style joins which makes it harder to read. You could rewrite it as (let's assume RealTimeVending was the current database):
FROM rtv_turnover_transaction as t
inner join rtv_trans_articles as ta on t.transaction_id=ta.transaction_id
inner join articles as art on art.id=ta.article_id
inner join groups on groups.id=art.group_a_id
inner join Clients as s on s.id=t.cliente_id
inner join rtv_transactions as tr on tr.id=t.transaction_id
cross join tills as till
inner join Ubicacion as u on t.ubicacio_id=u.id
inner join Operadores as o on t.operador_id=o.ID
where
--convert(date,t.trans_date) >='"+globalMap.get("datainici")+"'and
--convert(date,t.trans_date) <= '"+globalMap.get("datafinal")+"'and
--and (s.codigo IS NULL or s.codigo like '%"+globalMap.get("client")+"%')
--and (t.total_amount IS NULL or t.total_amount like'"+globalMap.get("amount")+"%')
Here you have inner joins with many tables and one CROSS JOIN that is completely (not only) useless. That cross join by itself is a cause for slowness. With a cross join, say:
t1 cross join t2
If t1 has 1000 rows and t2 has 10000 rows, you get 1000 * 10000 = 10,000,000 rows where each row on t1 is repeated 10,000 times (and t2 rows 1,000 times).
Next question is when you need fields from only rtv_turnover_transaction why do you join other tables? It may be on purpose no way to know without knowing schema and necessities. Likely they were used only for the purpose of (if EXISTS) check. If so you could add them as EXISTS queries in WHERE.
Then comes your WHERE clauses:
where
convert(date,t.trans_date) >='"+globalMap.get("datainici")+"'and
convert(date,t.trans_date) <= '"+globalMap.get("datafinal")+"'and
and (s.codigo IS NULL or s.codigo like '%"+globalMap.get("client")+"%')
and (t.total_amount IS NULL or t.total_amount like'"+globalMap.get("amount")+"%')
Here why the:
convert(date,t.trans_date)
That would nullify the use of an existing index on trans_date and make it slow. Instead:
t.trans_date >='"+globalMap.get("datainici")+"'and
is just fine if the globalMap.get("datainici") is returning a date or a datetime where time part is 00:00:00.
Then comes the most important one:
where
convert(date,t.trans_date) >='"+globalMap.get("datainici")+"'and
convert(date,t.trans_date) <= '"+globalMap.get("datafinal")+"'and
and (s.codigo IS NULL or s.codigo like '%"+globalMap.get("client")+"%')
and (t.total_amount IS NULL or t.total_amount like'"+globalMap.get("amount")+"%')
You should NEVER build SQL queries like this by concatenation of strings. This is not only a well known SQL injection attack cause also it might cause you to define parameters wrong. Simple cure is to use PARAMETERS.
Then would come other optimizations.
s.codigo like '%"+globalMap.get("client")+"%'is going to cause a problem ,full wildcard searches like that will always scan dataJOINsyntax! Easier to write (without errors), easier to read and maintain, and easier to convert to outer join if needed.talendbut the query would contain parameter markers with the actual run-time values added to the command separately (e.g.convert(date,t.trans_date) >= ?). It would be best to refactor the query in order to avoid applying functions likeconvertto the column so that indexes can be used efficiently.