1

I have six tables, which unfortunately does not have any primary/foreign key-relations encoded. I've tried to create a view, however I am more than happy with just a table.

I've managed to join the six tables, but after returning 33.5 million rows (cleanInventtrans has 1.7 million rows and every time more tables are added, I am assuming the tables are multiplied by the combination of the previous table) I run out of memory. Now, I realize this is not the correct way of doing this, if it was I'd get a result I am assuming.

I've looked at a couple of questions online
http://www.daniweb.com/web-development/databases/ms-sql/threads/123446/problem-using-join-with-six-tables
SQL joining 6 tables
Unable to relate two MySQL tables (foreign keys)

And I've looked at
http://www.techonthenet.com/sql/joins.php

However, they assume there is a primary-foreign key relationship, my tables do not have this but there are fields which correspond, between the various tables, as seen in the SQL code below. I use these to match up the tables, but somewhere along the line, I am clearly doing it wrong.

I understand that by doing it like this I am essentially multiplying results with each table I join with. I was hoping there was a more clever/correct way of doing it which reduces the amount of results to a management size.

I am unfortunately unable to provide a create statement.

View design with 6 tables. The code I am using:

SELECT     dbo.AX_SALESLINE.SALESID, dbo.AX_SALESLINE.ITEMID, dbo.AX_SALESLINE.QTYORDERED, dbo.AX_SALESLINE.SALESPRICE, dbo.AX_SALESLINE.LINEPERCENT, 
                  dbo.AX_SALESLINE.LINEAMOUNT, dbo.AX_SALESLINE.SALESQTY, dbo.AX_SALESLINE.CONFIRMEDDLV, dbo.CleanInventTrans.COSTAMOUNTPOSTED, 
                  dbo.CleanInventTrans.DATEPHYSICAL, dbo.AX_CUSTPACKINGSLIPJOUR.DELIVERYDATE, dbo.AX_SALESTABLE.CUSTACCOUNT, dbo.AX_SALESTABLE.SALESTYPE, 
                  dbo.AX_SALESTABLE.SALESSTATUS, dbo.AX_CUSTPACKINGSLIPJOUR.QTY, dbo.AX_PRODTABLE.PRODID
FROM         dbo.AX_CUSTPACKINGSLIPJOUR INNER JOIN
                  dbo.AX_SALESTABLE INNER JOIN
                  dbo.CleanInventTrans INNER JOIN
                  dbo.AX_INVENTTABLE ON dbo.CleanInventTrans.ITEMID = dbo.AX_INVENTTABLE.ITEMID INNER JOIN
                  dbo.AX_PRODTABLE ON dbo.AX_INVENTTABLE.ITEMID = dbo.AX_PRODTABLE.ITEMID INNER JOIN
                  dbo.AX_SALESLINE ON dbo.AX_INVENTTABLE.ITEMID = dbo.AX_SALESLINE.ITEMID ON dbo.AX_SALESTABLE.SALESID = dbo.AX_SALESLINE.SALESID ON 
                  dbo.AX_CUSTPACKINGSLIPJOUR.SALESID = dbo.AX_SALESLINE.SALESID

Edit: Salesline and salestable can be related via P/F-keys, however inventtable and salesline does not have a relationship beyond having a column named itemid, which is the same in both. But these have duplicates and can not function as keys.

Edit2:

Salesline and inventtable cannot be related for some reason:

'AX_INVENTTABLE' table saved successfully
'AX_SALESLINE' table
- Unable to create relationship 'FK_AX_SALESLINE_AX_INVENTTABLE'.  
The ALTER TABLE statement conflicted with the FOREIGN KEY constraint   "FK_AX_SALESLINE_AX_INVENTTABLE". The conflict occurred in database "VS1", table  "dbo.AX_INVENTTABLE", column 'ITEMID'.

I've checked the itemid in inventtable, it is unique, and there are multiple of the itemid in the salesline - however not all ID's are numerical in nature, some contain letters. I am not sure if this is what is causing the problem yet.

Edit3:
Foreign-primary relation fails because there are IDs in salines which are not present in the inventtable. Very odd.

1 답변


2

Don't bring back all of the rows from these 6 tables. If there are 1million plus rows in some of the tables at least filter on one of these tables, i.e.the CleanIventTrans table using a WHERE clause. If you're not going to add primary and foreign keys, at least add indexes to help improve performance.


  • Its not my database, its a Dynamics AX database, I am assuming the Primary/foreign keys are in the application layer instead. If i filter out rows, i wont get the necessary data to run my analysis on. I am not even sure how to add primary/foreign keys to this, without having to create each ID my self. - Tommy
  • For performance it would be best to add Primary/Foreign keys at the database layer. Why do you need to analyse 33.5 million rows at a time? To add foreign keys you could execute: ALTER TABLE YourTable ADD FOREIGN KEY (P_Id) REFERENCES TABLETOREFERENCE(Field) - Darren
  • Well, what I am trying to do is create a table which contains all the relevant columns for an analysis I am trying to do. The way the table is done so far, has resulting in over 30 mil rows, and then it runs out of memory - so its not so much a question of wanting to analyse the 30 mil rows, but more the desire of wanting a table which consist of the relevant columns, which are unfortunately spread out across 6 different tables. I've added a PF relationtion to two tables, however i get an error when trying to do so with saleslines and inventtable - Tommy
  • I have noticed in your code you are using FROM dbo.AX_CUSTPACKINGSLIPJOUR INNER JOIN dbo.AX_SALESTABLE INNER JOIN In your INNER JOIN specify the field to JOIN on, i.e. SALESID and see if this filters out the rows. - Darren
  • Ah, okay I've started doing the foreign primary key relations, and its coming along now. At some point when I've done them all, I'll have to figure out how t make all of them join using the least amount of rows and resources. Ill keep in mind that i should place a field to join on in each of the joins. - Tommy

Linked


Related

Latest