A statement which takes longer than expected has one of two causes, and the product tells them apart: it is making many calls, or it is making one call which is slow. Everything else follows from which of the two it is. ## Finding Out Which - [[Invantive Query Tool/Progress|Progress]] draws the statement while it runs, a card per step, with the rows which passed and the time each step spent. A step which is busy while the others wait is the answer. - [[Invantive Query Tool/Connection Monitor|Connection Monitor]] lists the calls with their duration, their round trips and their bytes. "Group by Request" adds them up per request: one line saying 400 calls answers a different question from 400 lines. - [[Invantive Query Tool/History|History]] states afterwards what each statement of this session cost, which is how a statement is compared with itself after a change. ## Many Calls A cloud platform is read a page at a time and written a row at a time, so the number of calls follows from what is asked for: - narrow with a `where` the platform can apply, so fewer pages are fetched; - name the columns rather than `select *`, since some platforms return less when less is asked for; - put the better-filtered side of a join first; see [[Invantive Query Tool/Joining Tables|Joining Tables]]; - select fewer partitions, since every partition is read separately; see [[Invantive Query Tool/Partitions|Partitions]]; - read what does not change often from the [[Invantive Query Tool/Data Cache|data cache]] instead of from the platform. ## One Slow Call Against a database the whole statement is usually one call, and its duration is the platform's. What helps then is what helps any database: a condition which uses an index, fewer columns, fewer rows. The statement sent is visible in the [[Invantive Query Tool/Connection Monitor|Connection Monitor]] under "Request", which is what is handed to the database administrator. ## What Is Not Worth Optimising The grid. Sorting, filtering and grouping in the [[Invantive Query Tool/Results|Results]] panel happen on rows which have already arrived and cost nothing on the platform. A statement is made faster by asking for less, not by arranging what came back; see [[Invantive Query Tool/Working with Large Results|Working with Large Results]].