I'm struggling with the performance of one MS-sql query, which I have to run to create a report in our ERP system.
Hopefully you can help me with that query.
Here is the query:
"Original version":
SELECT artikel.artikelnummer, artikel.bezeichnung, SUM(positionen.anzahl), artikel.Einheit
FROM artikel, auftrag, positionen
INNER JOIN auftrag AS auftrag1 ON (auftrag1.auftrag = positionen.auftrag)
INNER JOIN artikel AS artikel1 ON (positionen.artikel = artikel1.artikel)
WHERE
artikel.warengruppe = 2
OR artikel.warengruppe = 1234
OR artikel.warengruppe = 1235
OR artikel.warengruppe = 1236
OR artikel.warengruppe = 1237
OR artikel.warengruppe = 1239
OR artikel.warengruppe = 1240
OR artikel.warengruppe = 2139
AND auftrag.lieferscheinnr IS NOT NULL
GROUP BY artikel.artikelnummer, artikel.bezeichnung,artikel.Einheit
"Translated version":
SELECT article.articlenr, article.description, SUM(positions.amount), article.unit
FROM article, order, positions
INNER JOIN order AS order1 ON (order1.order = positions.order)
INNER JOIN article AS article1 ON (positions.article = article1.article)
WHERE
article.materialgroup = 2
OR article.materialgroup = 1234
OR article.materialgroup = 1235
OR article.materialgroup = 1236
OR article.materialgroup = 1237
OR article.materialgroup = 1239
OR article.materialgroup = 1240
OR article.materialgroup = 2139
AND order.dordernr IS NOT NULL
GROUP BY article.articlenr, article.description,article.unit
We want to count the amount of ink about all of our delivery orders (DO). In the table "auftrag" I have all of the DO numbers and the ordernumbers. In the table "positionen" I have all positions of the several orders including the right amount of inkbottles. In the table "artikel" I have all of the article-details like description, bottle size and so on. The column "artikel.warengruppe" contains the right material groups which contains the ink.
Now the problem is that the tables "auftrag" contains 160.000, "artikel" 155.000 and positionen 570.000 entries.
I abort the query after 1 hour runtime. So my question is how can I optimize the query?
My problem is that I cant change the ER-model.
Thank you very much in advance for your help. I hope you can understand my crappy english. ;)