• onemangathers - Wednesday, April 12, 2017 4:53 PM

    I don't understand the last part of your question in regards to the 4 part naming. Are you referring to the aliases just?

    Do away with the OPENQUERY and rewrite the query - the FROM clause would start like this:

    FROM SAASUFFOLK.MyDatabase.MySchema.billing_data bd
    JOIN SAASUFFOLK.MyDatabase.MySchema.billing_device_mapping bd
    ...

    Here are a couple of tips that won't make your query run any faster but will make your code more readable and resilient:
    (1) Get rid of those square brackets.  They're not necessary and they make the code harder to read.  Only use them for identifiers that are reserved keywords or that contain special characters (which, if your database was properly designed, won't happen).
    (2) Alias all of your columns.  There's nothing more irritating than having to scan through someone else's code trying to find which table the ACCOUNT_NUMBER belongs to.  Worse, and I have seen this happen, if a column of the same name is added to another table in your query, the code will break.
    (3) Lay out your code so that it's readable, like bmg002 showed you.  If you don't care about your code, why should anyone else?  You can format it manually or with a tool such as the one he mentioned.  That horrendous line with no spaces in it has messed up the line breaks on this page.

    Finally, please will you post the actual execution plan for this query when you run it direct on the remote server?

    Edit - I notice you posted in the Working with Oracle forum.  Is SAASUFFOLK an Oracle server?

    John