Adding MAXDOP to Linq to Entities - sql-server

I was looking for linq to entities extension which allows to add OPTION(MAXDOP x) to the query generated. For queries I want to limit their SQL Server resources.
Something like:
Customers.WithMaxDop(2).Where(...) ..
Couldn't find.
Before I try to dig-in to create my own extension I wanted to ask you guys first for help - how would you suggest to do so?
Thanks!

That is query hint which cannot be added by extension method. You must either build whole new EF provider or wrap the query with the hint into database view and map the view as the new read only entity.
EF is abstraction on top of database (theoretically any database) - it is not supposed to offer you control over such DB details. If you want these details you must code them on database layer and only expose views or stored procedures to EF.

It seems that it is now possible with EF Core 3.x.
You can "Intercept Database operetation" at a low level, before and/or after the operation. In the example provided by Microsoft, they added an hint at the end of the query.
command.CommandText += " OPTION (OPTIMIZE FOR UNKNOWN)";
However, I don't know if this will occur for each and any operation, or if you can apply those interceptions only on selected commands.
More info here: https://learn.microsoft.com/en-us/ef/core/what-is-new/ef-core-3.x/#interception-of-database-operations

Related

SYMFONY 6 - DOCTRINE : mapping and import only some table from an existing databse

I'm actually looking to map and import an existing database into a symfony 6 project.
I know we can do this by using this command :
php bin/console doctrine:mapping:import "App\Entity" annotation --path=src/Entity
But, this database is very huge and have a lot of tables. I don't want them all.
Do you know a way to "select" the tables i want to map. I know the tables that i don't want start with " _ " or " inv_ ". Perhaps there is a way to have a "where" clause ?
There is a --filer= option in doctrine:mapping:import but I think it's not what you're looking for.
If you migrate your codebase to symfony and doctrine and doctrineORM wasn't used before - it would be much easier just to start over and do all by your self. Yes, it's tedious, especially with a huge database, but you will end up with much "cleaner" entities and during this phase you could decide which tables to ignore.
But if you still want try to "import" somehow, consider following steps:
In your local developer environment, create a copy of your database. Just the schema without data. So you have all tables, but they're empty (how to with mysqldump Don't forget to use --no-data)
drop all other tables which you don't want
rename some, if you think, their names wouldn't fit to doctrine's naming convention.
switch to that copy-database in your .env (change db name in DATABASE_URL)
Now try to import again with doctrine:mapping:import. You may need to adjust some tables by repeating step 2) and 3) and then try to import again.
If import succeed, and you have a bunch of entities, now comes the boring and tedious part. You have to manually check all classes in src/Entity.
Depending on your Database (mysql, postgreSQL, sqlite, etc) not all column-types will be exact what you want.
Furthermore, most many-to-one/one-to-many relations and all many-to-many junction tables will be probably converted to standalone Entities like src\CategoryToProduct.php - which isn't right. So you have to delete them and recreate your relations by hand
If you happen to have a diagram of your database done with MySQL Workbench, you can use these tools to export your diagram into working entities :
First you setup this with composer: https://github.com/mysql-workbench-schema-exporter/mysql-workbench-schema-exporter
And then this : https://github.com/mysql-workbench-schema-exporter/doctrine2-exporter
Then you run the following command to get to know more how to use it :
php composer require --dev mysql-workbench-schema-exporter/doctrine2-exporter
You have plenty of options to parameter desired output, entities namespace and so on, everything is described in the second github rep if you want to use the doctrine2 ORM export format.

SymmetricDS synch from view

I'm looking at the features of SymmetricDS (last version symmetric-server-3.7.24) and in their forum I read it is actually possibile to synch from a view.
So I tried to synch from a view but when I run the program I got an error because symmetricDs cannot create a trigger on the view.
I also read that if a use a materialized view, then the trigger should be created.
The view is on a sqlserver 2008. I dropped the view and create a new one with schemabinding and add a cluster index on it. I also check that all the options are set as required in the MSDN guide to create indexed table.
I run symmetricDS again but still fail to create the trigger on the view.
Can anyone help me?
If what I ask is actually not possibile, then it is possibile to craete an extension that does not use trigger to synchronized the tables? I don't care that the two db are synched realtime, I can use a scheduled job, it will be just fine.
Thank you for you help and suggestion.
BTW: I can also change tool you you know a better one :)
I don't think that's a supported use case. However, you can try setting the sync_on_insert/update/delete fields to 0 on the sym_trigger. Then you would be able to sync the view with an initial load or by scheduling reloads (see "symadmin reload-table" command).

Create tabels in Hibernate auto or manually?

Im currently developing a servlet homepage (spring + hibernate + mysql).
Im at the moment using the Hibernate property hibernate.hbm2ddl.auto set to update.
This is working fine and Hibernate creates and updates my tables.
However, Ive have read on multiple places that this is not recommended in production and that it is unsafe.
But if I dont put this option my tables is not created, and I really don't want to create my tabels manually on the server. I got limited time working on this alone.
How is this usually done? It's seems like it is quite much work to add all tables manually imo.
In production, you typically have already existing tables with a large amount of data that you don't want to lose, and that you want to migrate to the new schema. Hibernate can't do that automagically for you. It doesn't know that the data that was previously in column A must now be in the new column B.
So you'll need to create a migration script. Of course, you can use Hibernate to generate the new schema for you in development, see what the differences with the old schema are, and create your script thanks to that. But yes, having an app in production and migrate it needs some work to be done.

Using liquibase, how to handle an object model that is a subset of database table

Some days I love my dba's, and then there is today...
In a Grails app, we use the database-migration plugin (based on Liquibase) to handle migrations etc.
All works lovely.
I have been informed that there is a set of db administrative meta data that we must support on every table. This information has zero use to the app.
Now, I can easily update my models to accommodate this. But that answer is ugly.
The problem is now at each migration, Liquibase/database-migration plugin, complains about the schema and the model being out of sync.
Is there anyway to tell Liquibase (or GORM) that columns x,y,z are to be ignored?
What I am trying to avoid is changesets like this:
changeSet(author: "cwright (generated)", id: "1333733941347-5") {
dropColumn(columnName: "BUILD_MONTH", tableName: "ASSIGNMENT") }
Which tries to bring the schema back in line with the model. Being able to annotate those columns as not applying to the model would be a good thing.
Sadly, you're probably better off defining your own mapping block and taking control of the Data Mapper (what Hibernate essentially is) yourself at this point. If you need to take control of the way the database-integration plugin handles migrations, you might wanna look at the source or raise an issue on the JIRA. Naively, mapping your columns explicitly in the domain model should allow you to bypass unnecessary columns from the DB.

django : about external database model

I'm using Django + internal database(mysql).
but, I need to query to another external database(mysql).
In case, Can I make to model of external database?
The database is already exist. Only need to query..
Thank you.
I think you'll need to take a look at this documentation. It explain how to register multi databases and then query from them. https://docs.djangoproject.com/en/dev/topics/db/multi-db/
To be specific from which DB you query you can jump to this part of the docs https://docs.djangoproject.com/en/dev/topics/db/multi-db/#manually-selecting-a-database-for-a-queryset

Resources