简体   繁体   中英

Stored Procedure and Entity Framework Insert Query - Performance

I have a simple Insert statement, but the table that I am inserting data into has millions of rows. The problem is that this Insert statement is being executed every 2 -3 seconds.

My CPU usage sometimes reaches 80%+ usage, due to this insert statement.

I need to ask that, if I write a stored procedure for this insert, will it increase my performance?

Stored procedures are stored in the SQL so there could be a performance improvement because SQL knows what procedures it has and then it can optimize the execution. Furthermore Entity Framework presents some kind of wrapper around database functionality. That means that it is a little bulky, because all the classes and methods/properties. Try it, it could bring an improvement.

That said, I don't think your problem is because of EF vs Stored procedures dilemma. It could be due to millions of records in the table. When you are performing an INSERT , the database checks all kinds of things, such as foreign keys, constraints, triggers are triggered etc. Other things, that could influence the performance and are database related, such as the size of transaction log etc.

I would try two things:

  1. I would create an identical table with identical triggers, indexes, primary keys, foreign keys, ... except I would leave it empty. I would then try if your current application still takes forever when you do INSERT . If it works fast, then it is probably database related.

  2. If the previous point wouldn't work, I would assume, there is a problem with the code, so I would create a Stored Procedure and try it again. If it would work OK, then I would change my code to use Stored Procedures and consult the DB admin about your particular situation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM