mariadb-corporation / mariadb-powerbi

Power BI DirectQuery Connector
https://mariadb.com
3 stars 1 forks source link

Connection to large MariaDB hangs on Foreign Key Detection #19

Closed ChristopherUseless closed 2 years ago

ChristopherUseless commented 2 years ago

When running on a database with few tables, connecting powerBI to MariaDB works like a charm. But when the number of tables grows, PowerBI becomes unresponsive, and can't open the smallest of tables. The problem is the way PowerBI checks for foreign keys between tables. When loading a preview of a table, PowerBI starts the query below. If you look at this query, you will see that it looks for (possibly missing) matches between all tables in the server, the table itself included. Even if the A.REFERENCED_% fields are empty (= NULL), the query looks for any possible matches to other tables in the server, causing an N^2 combination to look through. Similar for the REFERENTIAL_CONSTRAINTS. As you see, this quickly becomes a huge resultset to scan, even if the referenced A.column_names are empty. This check is performed on the tables your user have access to, so limiting access can reduce the problem. But still this is a rather unnecessary check, where it checks the relations between hundreds of tables, even if you only ask for data in 1 table. The query seems to be the same when it enters the MariaDB, regardless if PowerBI has connected via ODBC or using the connector provided by MariaDB. Are anyone in here familiar with this problem, and more importantly, does anyone have a workaround that is better than splitting our databases over multiple servers and/or limiting user access?

I see that other ODBC connectors such as cData have an attribute "EnableForeignKeyDetection" which is by default set to False, this would probably solve this issue. https://cdn.cdata.com/help/SRG/powerbi/RSBHelpinfo_p_EnableForeignKeyDetection.htm

Enclosed is the code that enters our MariaDB: SELECT A.REFERENCED_TABLE_SCHEMA PKTABLE_CAT, NULL PKTABLE_SCHEM, A.REFERENCED_TABLE_NAME PKTABLE_NAME, A.REFERENCED_COLUMN_NAME PKCOLUMN_NAME, A.TABLE_SCHEMA FKTABLE_CAT, NULL FKTABLE_SCHEM, A.TABLE_NAME FKTABLE_NAME, A.COLUMN_NAME FKCOLUMN_NAME, A.POSITION_IN_UNIQUE_CONSTRAINT KEY_SEQ, CASE update_rule WHEN 'RESTRICT' THEN 1 WHEN 'NO ACTION' THEN 3 WHEN 'CASCADE' THEN 0 WHEN 'SET NULL' THEN 2 WHEN 'SET DEFAULT' THEN 4 END UPDATE_RULE, CASE DELETE_RULE WHEN 'RESTRICT' THEN 1 WHEN 'NO ACTION' THEN 3 WHEN 'CASCADE' THEN 0 WHEN 'SET NULL' THEN 2 WHEN 'SET DEFAULT' THEN 4 END DELETE_RULE, A.CONSTRAINT_NAME FK_NAME, 'PRIMARY' PK_NAME, 7 AS DEFERRABILITY FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE A JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE B ON **(B.TABLE_SCHEMA = A.REFERENCED_TABLE_SCHEMA AND B.TABLE_NAME = A.REFERENCED_TABLE_NAME AND B.COLUMN_NAME = A.REFERENCED_COLUMN_NAME)** JOIN INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS RC ON **(RC.CONSTRAINT_NAME = A.CONSTRAINT_NAME AND RC.TABLE_NAME = A.TABLE_NAME AND RC.CONSTRAINT_SCHEMA = A.TABLE_SCHEMA)** WHERE B.CONSTRAINT_NAME= 'PRIMARY' AND A.TABLE_SCHEMA = BINARY 'SOMEDATABASE' AND A.TABLE_NAME = BINARY 'sometable' ORDER BY FKTABLE_CAT, FKTABLE_SCHEM, FKTABLE_NAME, KEY_SEQ, PKTABLE_NAME;

ChristopherUseless commented 2 years ago

Found a solution finally, that hopefully could be implemented in the MariaDB connector.
Source = Odbc.DataSource("Driver={MariaDB ODBC 3.1 Driver};Server=YourServer;Port=3306"";", [CreateNavigationProperties=false])

The last part, [CreateNavigationProperties=false] solves the whole issue, instead of staging all the tables when they are read into your model, where linked tables and records are shown as links that can be opened or extracted, you will now have to create the realtionships yourself. So you get less help, but you get a "normal" relational database where you join your tables by choice. It will load data in no-time, with this solution.

ilyagithub1 commented 2 years ago

@ChristopherUseless Thank you for filing the issue and suggesting the solution.

MariaDB data source configuration dialog box has been updated to include CreateNavigationProperties as an optional parameter. The value provided in this UI is passed to the Odbc.DataSource function. For usability reasons it has been given a more informative title of Auto-detect foreign key dependencies:

Issue-19-new-UI

The link to the commit that addresses this issue: https://github.com/mariadb-corporation/mariadb-powerbi/commit/1bf216a3acf476e7facaddd78f4e46857623cf78

Here is the assembled MEZ file that you can use to test the new UI: MariaDB.zip

Put the MEZ file to your Documents\Power BI Desktop\Custom Connectors folder. Create the Custom Connectors folder if it does not exist yet.

Issue-19-CustomConnectors

After the MariaDB.mez file is copied to the Custom Connectors folder, restart Power BI Desktop. To help you make sure you are using the updated MariaDB Power BI Connector, it will be displayed as MariaDB (Beta) in the Get Data dialog in Power BI Desktop.

When done with testing, the MEZ file can be safely deleted from the Custom Connectors folder. After deleting the MariaDB.mez file, restart Power BI Desktop again and it will revert to using the original version of the MariaDB Power BI Connector that has been shipped with Power BI Desktop.

This fix will be incorporated into a future version of Power BI Desktop.

ChristopherUseless commented 2 years ago

Hi Thanks a lot for the fix and for the Beta version. I just tested it, it from first glance it works like a charm. I also liked the explanation behind the information sign, that was well written and explained the use very well.

I really have to give you credit for the quick response and the making of this beta, that was truly impressive!!!

Have a great day!

BR Christopher


From: Ilya @.> Sent: Sunday, June 5, 2022 8:17 PM To: mariadb-corporation/mariadb-powerbi @.> Cc: ChristopherUseless @.>; Mention @.> Subject: Re: [mariadb-corporation/mariadb-powerbi] Connection to large MariaDB hangs on Foreign Key Detection (Issue #19)

@ChristopherUselesshttps://github.com/ChristopherUseless Thank you for filing the issue and suggesting the solution.

MariaDB data source configuration dialog box has been updated to include CreateNavigationProperties as an optional parameter. The value provided in this UI is passed to the Odbc.DataSource function. For usability reasons it has been given a more informative title of Auto-detect foreign key dependencies:

[Issue-19-new-UI]https://user-images.githubusercontent.com/42575359/172063484-3777526d-eb95-491e-bdb6-d5a9665c2e1b.png

The link to the commit that addresses this issue: 1bf216ahttps://github.com/mariadb-corporation/mariadb-powerbi/commit/1bf216a3acf476e7facaddd78f4e46857623cf78

Here is the assembled MEZ file that you can use to test the new UI: MariaDB.ziphttps://github.com/mariadb-corporation/mariadb-powerbi/files/8840450/MariaDB.zip

Put the MEZ file to your Documents\Power BI Desktop\Custom Connectors folder. Create the Custom Connectors folder if it does not exist yet.

[Issue-19-CustomConnectors]https://user-images.githubusercontent.com/42575359/172064173-54214f79-628a-4f94-a59f-3d27c277c8e7.png

After the MariaDB.mez file is copied to the Custom Connectors folder, restart Power BI Desktop. To help you make sure you are using the updated MariaDB Power BI Connector, it will be displayed as MariaDB (Beta) in the Get Data dialog in Power BI Desktop.

When done with testing, the MEZ file can be safely deleted from the Custom Connectors folder. After deleting the MariaDB.mez file, restart Power BI Desktop again and it will revert to using the original version of the MariaDB Power BI Connector that has been shipped with Power BI Desktop.

This fix will be incorporated into a future version of Power BI Desktop.

— Reply to this email directly, view it on GitHubhttps://github.com/mariadb-corporation/mariadb-powerbi/issues/19#issuecomment-1146860048, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AW2SYNKQ6BIAHGFJKEI22J3VNTVLHANCNFSM5XKMCXNQ. You are receiving this because you were mentioned.Message ID: @.***>

ilyagithub1 commented 2 years ago

Christopher, thank you for your feedback. Your feedback is highly appreciated.

I am glad to hear it solved your issue.

This fix should be incorporated into the upcoming Power BI Desktop August 2022 update. You may follow the updates at Microsoft Power BI Blog.

Thanks again and have a great day!