How to optimize query that returns data based on one matching and one missing field joining two tables

Hi all,
Here is what I am trying to do. I have 2 tables A and B. Both have a fiscal year and a school ID column. I want to return all data from table B where all school IDs match but fiscal year from A is not in B. I wrote 2 queries below but this took
2 minutes each to process through 30,000 records in table B. Need to optmize this query.
1) select 1 from table A inner join table B
on A.SchoolID=B.SchoolID where A.Year not in (select distinct Year from table B)
2) select distinct Year from Table A where School ID in (select distinct School ID from table B
and Year not in (select distinct Year from table B)

Faraz81,
query execution time will depend not only on your data volume and structure but also on your system resources.
You should post your execution plans and DDL to generate data structures so we can take a better look, but one think you could try right away is to store the results of the subquery in a table variable and use it instead.
You'll also benefit from the creation of:
1. An index for the B.SchoolID column.
2. Statistics for the Year column in table B.
You can also try to change the physical algorithm used to join A to B by using query hints (HASH, MERGE, LOOP) and see how they perform. For example:
select 1 from table A inner HASH join table B
on A.SchoolID=B.SchoolID where A.Year not in (select distinct Year from table B)
As the query optimizer generally chooses the best plan, this might not be a good idea though, but then again, without further information its going to be hard to help you.

Similar Messages

  • Updating a table with a query that return multiple values

    Hi,
    I'm trying to update a table which contain these fields : ItemID, InventoryID, total amounts
    with a query that return these values itemId, inventoryid and total amounts for each items
    Mind you, not all the rows in the table need to be updated. only a few.
    This what i wrote but doesn't work since the query return multiple values so i can't assign it to journalAmounts.
    UPDATE [bmssa].[etshortagetemp]
    SET JournalAmounts = (SELECT sum(b.BomQty) FROM [bmssa].[Bom] b
    JOIN [bmssa].[SalesLine] sl ON sl.ItemBomId = b.BomId
    JOIN [bmssa].[SalesTable] st ON st.SalesId = sl.SalesId
    WHERE st.SalesType = 0 AND (st.SalesStatus IN (0,1,8,12,13)) AND st.DataAreaId = 'sdi'
    GROUP BY b.itemid, b.inventdimid)
    Any advise how to do this task?

    Remember that link to the documentation posted above that explains exactly how to do this. When you read it which part exactly were you having trouble with?

  • I need a query that returns the average amount of characters for a text colum in MS SQL.

    I need a query that returns the average amount of characters
    for a text colum in MS SQL.
    Could someone show me how?

    Sorted, i need the
    DATALENGTH
    function

  • I own a ipad mini and a iPod touch , both configured on the same apple account, how do I ensure that my data on the ipad mail,notes,pics etc is not visible/accessible on the ipod

    I own a ipad mini and a iPod touch both configured on the same id
    How do I ensure that my data on the ipad mail,notes,pics etc is not visible/accessible on the iPod touch when the iPod is connected to a wi fi connection

    Mail: do not have the same account loaded onto the two devices.
    Photos: go into Settings->Photos & Camera->and turn off photo stream.
    everything else, sign out of the Apple ID on the device you dont want to be visible.
    good luck

  • After upgrading my iPhone, I lost all my contacts..... and they were replaced with my wife's contacts even though she upgraded after I did. I did a time machine backup before I started the upgrade process, how do I get that saved data back to my phone?

    After upgrading my iPhone, I lost all my contacts and they were replaced with my wife's contacts even though she upgraded after I did. I did a time machine backup before I started the upgrade process, how do I get that saved data back to my phone?

    Hi bluegrandpanash,
    Thanks for visiting Apple Support Communities.
    If you backed up your iPhone to iCloud before updating the software, first try the steps under "Restore from an iCloud backup" in this article to recover your data:
    iOS: Back up and restore your iOS device with iCloud or iTunes
    http://support.apple.com/kb/HT1766
    Best Regards,
    Jeremy

  • How to join two tables and get the supply delivery date next to order?

    So there are two tables. One has customer's order no, ordered date, order quantity, available quantity and code of article-
    The other table comes form supply side where we have supply order no, article number, ordered qty, and delivery date.
    We keep stock so this can not be MOT (made to order) system.
    What i need is correct date of arrival to appear next to cusotmers spoecirfic order. The older cusotmers order get's the parts first, second oldest order is next in line etc.
    here is any example
    customer's order
    ref order
    art. code
    ordered qty
    available qty
    order date
    1809202491
    700497
    60
    0
    3.7.2014
    1809200528
    700497
    13
    0
    20.6.2014
    1809198640
    700497
    7
    0
    9.6.2014
    supply order
    supply order
    art. code
    qty orderd
    date of arrival
    4501243378
    700497
    50
    4.8.2014
    4501263437
    700497
    20
    6.10.2014
    There is actually a 3rd "table" and that sort of connects the two and that is stock on hand per art. code.
    The main issue is that stock is assigned to purchase orders only when it actually arrives in the warehouse.
    A human can easilly connect the dates of when the stock will arrive and quantities with correct customer's order. In this case the firts order will get 50 pcs in August while 10 pcs will remain on backorders. The missing 10 pcs Will arrive in October. The second order will get 10 pcs in october and 3 will remain on backorders with no delivery date. While the third customer orders does not have a delivery date.
    So how to make the SAP do this calculations and display the arrival date next to date of customer's order?

    I checked the instructions as i do not have access to this part. It seem this is a query. We had issues with queries in the past as not all codes from orders would appear in them. They never found the reason why that is happening.
    However, I think the main issue is that the information here is not connected and is separately provided for supply and for sales. So i doubt it can be connected in this query.
    edit: as you can see the only connection is stock on hand.
    and total number of various items we have is close to 100.000 of various article codes.

  • Filtering report data based on user login and Parameter fields

    Post Author: mronquillo
    CA Forum: General
    Hi,I am running a report that filters data based on the user login. To do this, I created a formula called @user that compares the login name (using the CurrentCEUserName field) and returns the user's name. If the user login is not a login specified in the if statements, it returns the parameter field "user_name":For example: if CurrentCEUserName = "loginname1" then "User's Name 1"else if CurrentCEUserName = "loginname2" then "User's Name 2" else if CurrentCEUserName = "loginname3" then "User's Name 3"  else if CurrentCEUserName = "loginname4" then "User's Name 4".. .else {?user_name}   In select expert, I have a condition which filters data based on the string returned from that formula:{Table.Name} = {@user}  This works fine and when the users run the report they only their own data. However, they are still prompted to choose a parameter field regardless if of the value returned by the @user formula. Oddly enough, regardless of what parameter field they choose, they will still only see their own data (i.e. if John chooses "Bob" from the parameter list, he will still only see John's data.)If I remove the "else {?user_name}" line from the @user formula, then the users are not prompted anymore. However, if they are not a "valid" user - that is, if any of the if statements in the formula are not true for their login name - then they will see no data. What I want to do is make the report ONLY prompt the user to choose a parameter field if their login name is not "valid". That is, if the @user formula is able to return a string value for their login name, then they will jump right into the report without being prompted to choose a parameter - otherwise, the user will be prompted to choose a name from the parameter list. I thought my formula would allow this (hence the "else" clause), but it seems that if a parameter field is present in any formula, then the report automatically prompts the user to choose a parameter. Is what I am trying to accomplish possible in CR (I'm using CR v10.0) or is there a better way to do what I am trying to do?Thanks in advance.

    Post Author: sharonmtowler
    CA Forum: General
    try, or something like that
    (if CurrentCEUserName ={?user_name} then true else ({Table.Name} = {@user}) )

  • My iPhone 5s on ios 8.1.2 is stuck at Apple logo or recovery mode.. To fix this issue, i need to update it to ios 8.2 through itunes.. I want to ask that my data i.e. Photos and other personal data would be removed after updating it or not..?

    My iPhone 5s on ios 8.1.2 is stuck at Apple logo or recovery mode.. To fix this issue, i need to update it to ios 8.2 through itunes.. I want to ask that my data i.e. Photos and other personal data would be removed after updating it or not..?
    if yes then how can i save my data or get out of recovery mode.. Is there any safe mode or something like that on which i could access my data..
    P.S. I have not taken any backup of my iPhone on itunes..
    Please help me sort out this issue..

    Recovery mode doesn't mean that all your data is already lost. You only lose your data if you restore it.
    When you plug the phone is into iTunes hold down the home button. iTunes may then give you the option JUST to update the software first. This should not get rid of any data.
    However, if iTunes is only giving you the option to RESTORE and update, then you will lose your data I'm afraid.
    Hope that helps!

  • How to change it if some data is wrong in ODS and infocube

    hi all:
        could you please tell me how to change it if some data is wrong in ODS and infocube ?
    Best regards

    You receive information on all requests that have run in the InfoCube and you can delete requests if required.
    http://help.sap.com/saphelp_nw04s/helpdata/en/d4/aa6437469e4f0ae10000009b38f8cf/frameset.htm
    Request ID you can find in administration of the InfoCube. If a cubes is compressed you can't delete requestwise...
    Regards
    Andreas

  • After I run a query can I get it to return the number of files matched and each individual file name?

    After I run a query can I get it to return the number of files matched and each individual file name?  I am trying to do a data mining routine and this would be helpful. 
    BBANACKI

    Hi bbanacki,
    Please have a look at the following code:
    Define your queries and then:
    oAdvancedQuery.ReturnType=eSearchFile
    oMyDataFinder.Results.MaxCount = iMaxNumberOfReturndElements
    Call oMyDataFinder.Search(oAdvancedQuery)
    Set oMyResults  = oMyDataFinder.Results
    If oMyResults.IsIncomplete Then
      msgbox "The first " & str(oMyResults.Count) & " files found"
    Else
      msgbox str(oMyResults.Count) & "  files found"
    End If
    for iLoop = 1 to oMyResults.Count
      Cell.Text = oMyResults(iLoop).Properties("Name").Value
      Cell.Text = oMyResults(iLoop).Properties("fullpath").Value
    next
    Greetings
    Walter

  • I disabled my iPhone 4 and while doing a restore via iTunes,  it is downloading and timing out so the download is not completing. any idea how to get past that?  I have tried many times and the same error keeps happening.

    I disabled my iPhone 4 and while doing a restore via iTunes,  it is downloading and timing out so the download is not completing. any idea how to get past that?  I have tried many times and the same error keeps happening.

    You need to force the phone into recovery mode, as described here, & restore it:
    http://support.apple.com/kb/ht1808
    Sorry, you've already lost all data on your phone.

  • I am trying to change my AppleID but it is telling me that my date of birth doesnt match their records?? what can i do, since obviously their records are wrong

    I am trying to change my AppleID but it is telling me that my date of birth doesnt match their records?? what can i do, since obviously their records are wrong

    don't take this the wrong way, I know you are trying to help, but that wasn't helpful in the slightest. Yes I realise that they don't know me personally, and hence don't know what my actual birthdate is. When I fill in online forms I generally do them accurately. I have no idea how they have it wrong, but what I was really alluding to was, can someone please tell me how to contact Apple so they can do it manually. this is doing my head in royally, since I cant download any apps etc on any of my apple stuff, as my account has been frozen.

  • TS2634 What if your iPod touch is reading that the date is in the 1800s and will no longer update the web and music that is "recent"?

    My iTouch is reading that the date is in the 1800s and will no longer update my "recently purchased" songs or update websites.

    Go to Settings>General>Date & Time and correct the times/date. Also, in Settings>General>International make sure the Calendar is set to Gregorian.

  • Can anyone help me in iphone mobile, i had lost my data in phone, because i restore my phone without backup, how can i get my old data, i lost my photos and videos... help me

    can anyone help me in iphone mobile, i had lost my data in phone, because i restore my phone without backup, how can i get my old data, i lost my photos and videos... help me

    Sorry Raj,
    If you did not back up your information, and you have now restored your phone, then your information is not recoverable.
    Sorry,
    GB

  • How do I ensure that emails are deleted on my iPhone and Ipad when I delete them on my iMac

    How do I ensure that emails are deleted on my iPhone and other devices when I delete them on my iMac?

    Care to share which OS you are using?
    iOS is for mobile devices.

Maybe you are looking for

  • Production Order Simulation for Material Availability

    Please Help. Does anyone know how to run a simulation for a configured material and then do a material availability check? First I need to be able to configure a material, then I need to be able to see if I have the material available to build it. Th

  • New OracleConnection hangs in VS 2005 debug mode

    Client 10.2.3, ODT 10.2.0.21 for Vista, VS2005sp1, Vista Sp1 Everything has been working great until I tried to debug a service for the first time since last week. Now no matter what I do, a call to create a new OracleConnection locks up the VS debug

  • Error 550 5.7.0 Relay Access Denied in Server 2013 Enterprise

    Good evening everyone. When emails are sent from outside of our domain, to our domain; the following message is returned: Delivery to the following recipient failed permanently:      email Technical details of permanent failure  Google tried to deliv

  • 2 Game Center Accounts on 2 iPads - with the same Apple ID. How to do that?

    I really hope someone can help my daughter and I here... The story: My daughter and I had an iPad each - naturally, since she is a minor, both iPads had the same Apple ID & password. In Game Center, we had separate Accounts (so we wouldn't mess up ea

  • Problem connecting Firewire device and external HD at the same time

    Hi. I'm attempting to record digital audio through a Presonus Firepod A/D interface via a Firewire 400 to 800 cable connected to one of the Firewire 800 ports on a mid-2010 Mac Pro running Snow Leopard. The problem is that if I have an external HD co