问题
How to check which of the two DAX queries has a better performance using Daxstudio. In the example the queries return exactly the same results. However the stats differ showing not clear hints. What usuful information can we grasp from the comparison of the two queries?
Comparison query stats summary:
+-------------------------+------------+---------+---------+
| | | Query 1 | Query 2 |
+-------------------------+------------+---------+---------+
| Server timings | Total | 7 | 5 |
| | SE CPU | 0 | 0 |
| | FE | 6 | 4 |
| | SE | 1 | 1 |
| | SE Queries | 3 | 2 |
| | SE Cashe | 0 | 0 |
+-------------------------+------------+---------+---------+
| Query plan, no of lines | physical | 7 | 28 |
| | logical | 13 | 9 |
+-------------------------+------------+---------+---------+
- The second query is quicker but has a bushy and longer plan. 2 scans.
- The first query has longer server timings but cleaner and shorter query plan. 3 scans.
So the server timings favor the second query but its complex query plan raises concern. Knowing the stats and query plans what can we expect if the SearchTable had milion of rows? Shouldn't we like simpler query plans, since DAX optimization may change in the future in their favor?
Sample data. We have two tables SearchTable and ThisTable:
SearchTable =
DATATABLE (
"Category", STRING,
"Product", STRING,
"Amount", INTEGER,
{
{ BLANK () , "apple" , 1 },
{ "Fruits" , "apple" , 1 }, -- watch out for multiple apples!
{ "Yummy Fruits", "apple" , 2 },
{ "Fruits" , "banana" , 4 },
{ "Fruits" , "wolfberry" , 5 },
{ "Fruits" , "cherry" , 3 },
{ "Vegetables" , "carrot" , 3 },
{ "Vegetables" , "potato" , 1 },
{ "Vegetables" , "onion" , 7 },
{ "Fruits" , "cherry" , 3 }
}
)
---
ThisTable =
DATATABLE (
"Product", STRING,
{
{ "apple" },
{ "banana" },
{ "blackberry" },
{ "carrot" },
{ "cherry" },
{ "onion " },
{ "potato" },
{ "watermelon" },
{ "wolfberry" }
}
)
Query no 1.
EVALUATE
ADDCOLUMNS (
VALUES ( ThisTable[Product] ),
"FilterLookup",
VAR LookupKey = ThisTable[Product]
RETURN
CALCULATE ( MAX ( SearchTable[Category] ), SearchTable[Product] = LookupKey )
)
The query has these stats:
And query plan:
Query no 2.
EVALUATE
ADDCOLUMNS (
VALUES ( ThisTable[Product] ),
"FilterLookup", MAXX (
FILTER ( SearchTable, SearchTable[Product] = ThisTable[Product] ),
SearchTable[Category]
)
)
Stats:
The query plan:
The question is related to:
DAX lookup first non blank value in unrelated table
You can download a pbix file with sample data:
DAX lookup top 1 value.pbix
回答1:
It's hard to extrapolate performance from a tiny data set to a large one due to fixed overhead costs, so I'd recommend testing on larger data tables.
In general, you want to avoid iterators like MAXX
in favor of MAX
when possible as the latter has under-the-hood engine optimizations. Very few rules are universal in optimizing queries so this is a rather opinion-based question given the data you've shown.
回答2:
You can't really tell from DAX Studio, when the data sets are so small, but in most cases, the query with the least complex query plan will be fastest. This is the case for query #1 and that will indeed be the fastest query in your case (ignore all time measurements below ~20 ms - as it's unreliable, because the data sets are so small).
Also, I'd like to add that the following query should provide the same result, and be even faster, with an even simpler query plan than both of your queries:
ADDCOLUMNS(
ThisTable,
"FilterLookup",
LOOKUPVALUE(SearchTable[Category], SearchTable[Product], ThisTable[Product])
)
Edit: I didn't notice that "apple" appears twice in the SearchTable[Product]
column. This will cause the above call to LOOKUPVALUE(...)
to fail, since it won't be able to find an unambiguous value for SearchTable[Category]
.
来源:https://stackoverflow.com/questions/59883211/how-to-check-in-daxstudio-which-dax-query-has-better-performance