Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. query optimization

query optimization

Scheduled Pinned Locked Moved Database
databaseperformancequestionagentic-aialgorithms
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    pstsp911
    wrote on last edited by
    #1

    Hello, I have a sql (t-sql) question. I have two tables Orders and Agents. Each agent does a transaction (bying or selling) and transactions are recorded in Orders table. Columns in table "Orders": ItemId, bla, bla, bla, Seller, Buyer Columns buyer and seller are ID of responsible agent. Columns in table "Agents": AgentName, AgentSecondName, ID, bla, bla, bla NOW i want to display list of all agents alongside with numbers of sold and bought items, in a new table like this: AGentName, AgentSecondName, AgentId, Bought, Sold My query is SELECT AgentName, AgentSecondName, ID (SELECT COUNT(*) FROM Orders WHERE (Seller = Agents.ID)) AS SOLD, (SELECT COUNT(*) FROM Orders WHERE (Buyer = Agents.ID)) AS BOUGHT FROM Agents but itąs damn slow with a few millions of records that i have (although its still milliseconds its too slow), any idea how to speed it up?

    M P 2 Replies Last reply
    0
    • P pstsp911

      Hello, I have a sql (t-sql) question. I have two tables Orders and Agents. Each agent does a transaction (bying or selling) and transactions are recorded in Orders table. Columns in table "Orders": ItemId, bla, bla, bla, Seller, Buyer Columns buyer and seller are ID of responsible agent. Columns in table "Agents": AgentName, AgentSecondName, ID, bla, bla, bla NOW i want to display list of all agents alongside with numbers of sold and bought items, in a new table like this: AGentName, AgentSecondName, AgentId, Bought, Sold My query is SELECT AgentName, AgentSecondName, ID (SELECT COUNT(*) FROM Orders WHERE (Seller = Agents.ID)) AS SOLD, (SELECT COUNT(*) FROM Orders WHERE (Buyer = Agents.ID)) AS BOUGHT FROM Agents but itąs damn slow with a few millions of records that i have (although its still milliseconds its too slow), any idea how to speed it up?

      M Offline
      M Offline
      Mycroft Holmes
      wrote on last edited by
      #2

      There are a number of ways to get this data, I would probably use two left joins and a sum on a case statement based on the id in the seller/buyer fields. You would need to create the alternative queries and run them through the profiler to evaluate the performance. Performance is more likely to be improved by adding an index to the buyer and seller id fields on the orders table.

      Never underestimate the power of human stupidity RAH

      1 Reply Last reply
      0
      • P pstsp911

        Hello, I have a sql (t-sql) question. I have two tables Orders and Agents. Each agent does a transaction (bying or selling) and transactions are recorded in Orders table. Columns in table "Orders": ItemId, bla, bla, bla, Seller, Buyer Columns buyer and seller are ID of responsible agent. Columns in table "Agents": AgentName, AgentSecondName, ID, bla, bla, bla NOW i want to display list of all agents alongside with numbers of sold and bought items, in a new table like this: AGentName, AgentSecondName, AgentId, Bought, Sold My query is SELECT AgentName, AgentSecondName, ID (SELECT COUNT(*) FROM Orders WHERE (Seller = Agents.ID)) AS SOLD, (SELECT COUNT(*) FROM Orders WHERE (Buyer = Agents.ID)) AS BOUGHT FROM Agents but itąs damn slow with a few millions of records that i have (although its still milliseconds its too slow), any idea how to speed it up?

        P Online
        P Online
        PIEBALDconsult
        wrote on last edited by
        #3

        How's about this?

        SELECT ID=MAX(Agent.ID)
        ,FirstName=MAX(Agent.FirstName)
        ,LastName=MAX(Agent.SecondName)
        ,Sold=SUM(CASE Seller WHEN Agent.ID THEN 1 ELSE 0 END)
        ,Bought=SUM(CASE Buyer WHEN Agent.ID THEN 1 ELSE 0 END)
        FROM Agent
        LEFT OUTER JOIN [Order]
        ON Agent.ID=[Order].Seller
        OR Agent.ID=[Order].Buyer
        GROUP BY Agent.ID

        :-D (And, yeah, be sure to index Buyer and Seller)

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups