Showing posts with label Performance. Show all posts
Showing posts with label Performance. Show all posts

Monday, November 11, 2019

Power Query and The Curious Case of Keep Top Rows

The Power Query Editor in Power BI has many transformations that can be set from the UI. Keep Top Rows is one of them. I'd like to take you on my journey through this curious case I ran into a couple of weeks back when preparing for my session on Query Folding in Power BI.

Transformations in Power Query

Problem

I was preparing my demo's for my session at dataMinds Connect on Query Folding in Power BI (slides). If you're not familiar with query folding: transformations in Power Query are transformed into the native (data source) language and executed at the source for better performance. For a more thorough overview have a look at the post How Query Folding Works from Matt Allington (B | @ExceleratorBI).

If we look at SQL Server, some transformations can be folded (like Sorting, a simple Group By and Keep Top Rows), others cannot (merging two columns from the UI, using the Buffer() M-function and Keep Top Rows). See what I did there? :) Keep Top Rows on its own is a foldable transformation, it is (obviously) transformed into the TOP operator in SQL.
However, when using Keep Top Rows in combination with some other transformations it (looks like it) is not folding anymore. Your best chance is to use the View Native Query option in Power Query SQL Server Profiler to check if the query is folding or not. More on that later :)
A bit of contemplation: I do think the real use case of the Keep Top Rows transformation might be mostly in development environments, where you can limit the number of rows to import for example. I still think the findings were rather odd and worth sharing. So, let's have a look at how it works.

Setup

I used import mode for the Fact.Movement table from the WideWorldImportersDW database. I then added the Keep Top (100) Rows transformation in the Power Query Editor.
Keep Top Rows


As you can see it folds to SQL and all is good for now.

View Native Query with TOP 100






























Adding transformations


Let's add a transformation on the Quantity column and apply an absolute value to it.
Add Absolute value of Quantity

So now the option to View Native Query is gone. Let's see if Power Query is speaking the truth (spoiler: No) 😀
I started SQL Server Profiler and captured the queries running against the database after I Close and Apply my changes. As you can see, now both the TOP operator and ABS function are folded.
SQL Server Profiler: TOP 100 and ABS

Now let's try something different and add a filter on the Date Key. Something strange has happened, because I already filtered the first 100 rows, so only 2 dates remained after that. But when I click on the Calculated Absolute Value query step I suddenly have a lot more dates that shouldn't be there in my opinion.
Filter on Date Key


Let's add a field from the Customer dimension this time and see what happens.
Expand Customer Dimension

The query for Fact.Movement is still the same, so the TOP operator and ABS function are still folding. And this is what is sent to the database for the Customer dimension:
SQL Server Profiler: Customer Dimension with WHERE clause

The Curious Case

Now watch closely to the where clause: so it actually already filters out the Customer Key based on the steps performed earlier in the Query Editor. But this Customer dimension is a separate query that returns 1 row, which will be joined to the other query inside Power Query.
Back to that WHERE clause: that Customer Key = 0 is valid for my (broken) Calculated Absolute Value query step, not for the Kept First 100 Rows as you see in the picture above. So I actually get the wrong a different result back into Power Query than I would expect.
I see you're thinking: "But you didn't add an explicit order, right?" So the top 100 is a little arbitrary. That's true, so let's add that order by.
Add Sorted Rows

This is a slightly better result in my opinion. Because now the Power Query steps are in sync with each other. The issue with View Native Query still remains the same by the way. It stops working at the Calculated Absolute Value query step, but the query send to the database is the following, so including the ABS function:

View Native Query after Sort

Recap

I showed several variations in the Power Query Editor of using Keep Top Rows and showed that adding subsequent steps brakes the View Native Query option in the editor, although query folding sometimes does take place. And adding an order to your query before applying a TOP operator is always a good idea, not only in SQL.

The steps I used might not all be best practices when creating a data model in Power BI. In case of the join to the customer dimension for example, it might be better to model it as a separate dimension, because STAR SCHEMA ALL THE THINGS. But I used those steps to highlight the problem.

I already mentioned this 
bug feature to Patrick and Adam (members of the Power BI CAT team) from Guy in a Cube (B | @GuyInACube) when I was at the Precon at Data Saturday Holland. Patrick LeBlanc (B | @patrickdba) also hadn't seen this before and hopeully will take it up with one of his team members. I thought I'd make a write up for him to easily reproduce it.

Tuesday, April 9, 2019

My Power Platform Summit Europe Recap

Last week the Power Platform Summit Europe was held in Amsterdam. I was there together with my colleagues Sander and Pim and next to the learning experience the conference gave me, it was really nice to join them!

Before starting my recap I’d like to point you to the following tool: Microsoft Office Lens, it’s available both on iOS and Android! In every session I was in there were always people taking pictures of the slides. I sometimes do it also, just because I want to make a note next to the slide or because it’s something in a demo I’d like to capture. But the point I’m trying to make here: do it with the Office Lens app, because then your pictures will look like this:

The information/sessions that stood out the most for me:

Line Krogh talked about Brons, Silver and Gold Power BI report templates:

  • Gold: created by BI team on a managed Tabular model
  • Silver: not created by BI team on a managed Tabular model
  • Brons: not created by BI team on non-certified data sources

The Art of Gathering Effective BI requirements by Mico Yuk

  • An interactive session where she also shared her spreadsheet to be used in discussion with end-users

Nikhil Gakwaed showed us some nice improvements in the Power BI Service:

  • Comments on dashboards
  • Organizational branding for Power BI Home

Callum Green did a session where one part about data profiling was interesting in particular:

  • Data profiling in M (Table.profile)
  • He also did some tweaking afterwards to get the profiling on the column and value level

The Value of Self-serve BI in a Large Organisation by Rishi Sapra:

  • What-if scenario builder, which he will explain in-depth at the virtual Power BI Days

Demo of splitting the report and the data model by Just Blindbaek:

  • This will be even better when Shared and Certified datasets are available, hopefully later this month!

Power BI Performance by Kasper de Jonge, David Magar and Bhavik Merchant:

  • Avoid Bi-Di crossfiltering: you will go to hell! (according to Alberto Ferrari)
    • Instead turn crossfiltering on in DAX: CROSSFILTER(Dim, Fact, OneWay/Both)
  • Use variables, especially with IF
  • Don't use IFERROR
  • Use DIVIDE
  • Model Star-schema/Snowflake
  • Filter on Dimensions

A great session on Aggregation Tables by Reza Rad:

  • Import mode, Direct Query and Dual
  • Use DaxStudio to analyse the queries send by DQ


At the end of day 2 we had a nice view of the entrance to the conference:

In the morning I found some time to go running with Sander in the Amstel Parc just across the road from our hotel. Although it was very chilly, it was worth the early wake up!


Let me know if you agree with me or if you think you liked other sessions more at the Summit.

My next post will be a recap of the Power BI User Day 2019 in Utrecht where I also gave a presentation together with Jeroen Schalken.

@NickyvV

Monday, June 3, 2013

Suggested reading

I came across some nice articles and blog posts in the last weeks I wanted to share:

Hopefully you can get somehting useful out of the links!

You can also follow me on Twitter: @NickyvV

Wednesday, June 27, 2012

MSTechEd Europe 2012 Day 1: Optimizing your BI Semantic Model for performance and scale

I joined my first session on tuesday by Dave Wickert. Here's my short version of the session.

VertiPaq is all about Performance, Performance, Performance!
The VertiPaq engine chooses query performance over processor performance.
What the VertiPaq engine does:
  1. Encoding (per column): creates a dictionary with value or hash encoding (self-deciding)
  2. VertiPaq storage
    • dictionary
    • column segment
    • hierarchy
    • relationships
    • partitions
  3. Processing
    • Process Full
    • Process Data
    • Process Recalc (includes calculated columns)
  4. Advanced Processing
    • Process Defrag: updates the dictionary
    • Parallel processing
    • Error handling
  5. Server Memory Map
    • Databases
    • Formula engine (DAX, MDX)
    • VertiPaq caches
    • Storage engine
Defaults that can be adjusted (be careful):
VertipaqMemoryLimit - 60%
TotalMemoryLimit - 80%
VertipaqPagingMode

Tuesday, June 26, 2012

MSTechEd Europe 2012 Pre-Con: SQL Server Performance Tuning and Optimization - It all depends...

Yesterday I was at the Pre-conference Day at the MSTechEd Europe. I learned a lot during the session and tried to make some useful notes so I can use them for later reference. Specially the part on DMVs was very useful, I didn't use them before and certainly am checking them in the next weeks!

By Denny Cherry and Thomas LaRock

1. Identifying performance bottlenecks
Tools used:
- Performance Monitor
- Activity Monitor
- DB Tuning Advisor (DTA)
- Data collection
- Profiler
- SQLTrace
- Dynamic Management Views (DMVs)
- Extended Events (xEvents)
Blocking processes / wait events (running, runnable, waiting) aren't shown in Profiler (= SQLTrace + visualization). Those can be shown with the xEvents Wizard under Management in SSMS. Always tune it to your needs, never use a default (See PAL).
Summary:
- Define the problem
- Decide for tool with the best metrics and least (perf.) cost
- Consider the time needed to analyze
- It depends..
2. Index optimization
1. Indexing
- Started at design time
- Data types matter!
- Changes over time
- First line of defense for (quick) perf. Tuning
Do we have the right indexes?
- It depends..
- Follow best practices
- Use DMVs for missing indexes
- Focus on Logical IO (Inside SQL Server 2005: T-SQL Querying by Itzik Ben-Gan)
SQL Diagramming > table with highest selectivity of data
Index seek is better than Index scan.
If an index has only writes and no reads > delete the index
2. Execution Plans
Look for:
- Correlation with wait events
- Examine first operator (Select | Update | ..)
- Warnings (missing index)
- "More costly"  operators: LIKE > Spool uses tempdb
- "Fat fingers": thick arrows means lots of rows
- Looking at the #rows in
□ An index > update statistics
□ Hash match > input vs output rows (do nothing)
3. Filtered Indexes
An index with a filter (where clause), but no extended logic (e.g. OR)
4. ColumnStore Indexes
- Based on the xVelocity in-memory analytics engine (a.k.a. Vertipaq)
- CREATE COLUMNSTORE INDEX [COLUMNS]
- Downside: the table is read-only, workarounds:
□ Delete index > load data > create index
□ Use 2 tables, 1 read-only and 1 read/write table
3. Storage optimization
1. Table partitioning
- Reduce maintenance window
- Improve query response time
- Reduce recovery time
Create filegroups, a partition function and a scheme referencing the function.
Business (logic) knowledge is essential before starting partitioning!
When moving data: meta data (page headers) is updated
When deleting data: 
- Move partition to another table > then truncate (faster and save)
- Also getting data back that shouldn't be deleted is faster. Let the data stay in the other table for a few days before actually deleting it.
2. AlwaysOn (Enterprise feature)
- No need for shared storage
- Offload backups
- Scaling out read-only activity
4. Troubleshooting
- Are all queries running slow?
- Use xEvents with DMVs
- Is a subset of queries running slow?
- Use Executions plans with DMVs
Useful DMVs:
- Sys.dm_exec_query_stats
- Sys.dm_exec_query_plan
- Sys.dm_exec_cached_plans
STATISTICS IO ON
STATISTICS TIME ON
Using GO 10 after a SQL statement will execute the statement 10 times, useful when inserting data.


Resources mentioned can be found @ http://mrdenny.com/res/teched-2012-precon

Monday, May 21, 2012

Performance difference between DISTINCT and GROUP BY


I was wondering if the following two queries differ in performance when used in SQL Server.
select DISTINCT persNrDV, volgnrDienstverband from medewerker
select persNrDV, volgnrDienstverband from medewerker 
GROUP BY persNrDV, volgnrDienstverband

The goal of both of the above queries is to produce a list of distinct employee numbers and employment numbers from the employee table. The first query uses SELECT DISTINCT to accomplish this task, and the second query uses GROUP BY.
I ran these queries with the Execution Plan option turned on, I found that not only the results are identical, but the Execution Plans are also identical. The SQL Server Query Optimizer has the ability to decipher each query, determining what the ultimate results are, and to produce the most efficient Execution Plan possible.
So my conclusion is that there is no performance advantage using one form of the query over the other.

Friday, February 10, 2012

Hub'n'Spoke in PowerPivot - The Core and Thin Workbooks

I'm still working on the Management Dashboard I blogged earlier on when I was Implementing Parent / Child Hierarchy and dealing with the issue with AVERAGEX with SUM vs. SUMX with CALCULATE(AVERAGE).

I am now dealing with some performance issues, specially when opening and refreshing the Excel file. Another point occured when setting the auto refresh option in SharePoint. The refreshing itself is pretty fast, but when you open the file afterwards, it takes ages to load.. This has to do with the first (PP) and second (Excel) refresh of the file as Rob (PowerPivotPro) explains in his post.
When searching for solutions I came across some posts of Rob, the Core and Thin workbooks posts (1 and 2). The implementation of this hub'n'spoke model for PowerPivot is quite straight-forward with the help of Rob's posts.

In short, you have to get a copy of your workbook, change the connection from embedded to the location of the core workbook on Sharepoint. Then you can rename your thin workbook to .zip and copy an empty item1.data in xl\customData. Rename back to xlsx and your thin workbook is created.



Another performance issue I have is when opening an auto-refresh file in Sharepoint, it takes ages for the file to load, due to the second refresh where Excel has to refresh all the formulas and pivot tables. Everytime the file is opened this refresh takes place. With the Core-Thin concept this is not totally solved, because the thin workbook still has to be refreshed..
Still, I can now auto-refresh the core workbook in Sharepoint (this sets the autorefresh on open mark in the file). But the core never gets opened by the user, the thin only consults the data inside the core, so that's not a problem.
The only issue I now have is keeping the thin workbook up to date. Rob (or PivotStream) seems to have a solution for this problem, so I think patiently waiting is the only option.. :)

Featured Post

Power Apps or Translytical Task Flows?

I think I have gotten this question at least five or six times in the last few months, and with Translytical Task Flows reaching GA in the M...