Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Mateusz Kotwica 29 posts 129 karma points
    Mar 01, 2023 @ 00:02
    Mateusz Kotwica
    0

    Save and Publish is taking longer the more content I have in database

    Hello,

    I use Umbraco v8.18.3 and I have a process which serves to simply reads contnent (IContent), update it with some data and save and publish. Average number of items to process is around 800 documents.

    When I run it on my developer database, which has small amount of overall content inside, my process is very fast (8-11 documents per minute), hovewver when I run it on production DB (a lot of content) the same process is very slow (1 document per minute).

    I tested both databases on the same DB server instance to ensure the same circumstances. I can see strong corelation between performance and number of documents stored in DB.

    When I added metrics to the code I observed longer execution time on SaveAndPublish method. For small database it took around 0,5 second to complete operation, when for large database, it is around 6-8 seconds. I would like to mention here that I am saving excatly the same document and excatly the same data. The only difference here is the database where the data is stored.

    Do you have any ideas why the performance is related to amount of data stored inside db and how to tweak it?

  • Huw Reddick 1929 posts 6697 karma points MVP 2x c-trib
    Mar 01, 2023 @ 07:23
    Huw Reddick
    0

    Possibly indexing if you have a large number of content records

  • Mateusz Kotwica 29 posts 129 karma points
    Mar 01, 2023 @ 09:25
    Mateusz Kotwica
    0

    Thanks for response.

    Any suggestion which tables should I put indexes on?

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Mar 01, 2023 @ 09:30
    Dave Woestenborghs
    0

    HI Mateusz,

    How are you doing this. It could be something in your code that is causing this slow down.

    Dave

  • Mateusz Kotwica 29 posts 129 karma points
    Mar 01, 2023 @ 09:44
    Mateusz Kotwica
    0

    Thanks for your answer Dave.

    The trick is I'm running exactly the same process using exactly the same code and input data but on different databases: small and large one.

    I measured execution times for all my methods used to perform my process and it showed me clearly that the only difference is on ContentService::SaveAndPublish method.

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Mar 01, 2023 @ 09:47
    Dave Woestenborghs
    0

    Hi Mateusz,

    It could be that you have a mistake in your code which causes the SaveAndPublish to be slower on a larger content set.

    Dave

  • Mateusz Kotwica 29 posts 129 karma points
    Mar 01, 2023 @ 09:55
    Mateusz Kotwica
    0

    That is interesting.

    So in a nutshell: I'm getting the content using IContentService::GetById method. Than I update the property values using IContent::SetValue and finally I'm saving it using SaveAndPublish method. All done in accordance to the documentation.

    Could you please specify what kind of mistake is possible to make in this process, where should I look for?

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Mar 01, 2023 @ 09:59
    Dave Woestenborghs
    0

    Hi Mateusz,

    Could your post your code. That would make it easier to check if something where things are going wrong.

    Dave

  • Mateusz Kotwica 29 posts 129 karma points
    Mar 01, 2023 @ 11:24
    Mateusz Kotwica
    0

    As the whole process is spread over many services and classes, I had to simplify the algorithm a bit, but managed to include everything in concise methods and the most important - the issue still occur.

    So there is UpdateSimple method:

        private void UpdateSimple()
        {
            var item = _contentRepositoryWrapper.GetContentById(243059);
    
            if (item != null)
            {
                item.SetValue(Season.GetModelPropertyType(x => x.Title).Alias, "New Title");
    
                _contentRepositoryWrapper.SaveAndPublish(item, ImporterConfiguration.ImporterUserId);
            }
        }
    

    _contentRepositoryWrapper is a simple wrapper on Umbraco built-in services:

        public IContent GetContentById(int id)
        {
            using (var cref = _umbracoContext.EnsureUmbracoContext())
            {
                var content = _contentService.GetById(id);
    
                return content;
            }
        }
    
        public PublishResult SaveAndPublish(IContent content, int userId)
        {
            using (var scope = _scopeProvider.CreateScope(autoComplete: true))
            {
                var result = _contentService.SaveAndPublish(content, userId: userId);
    
                return result;
            }
        }
    

    And now please see the execution times. First one comes from method run on a small db:

    Small DB

    As you can see execution time is less than 3 seconds.

    Now the large db:

    Large DB

    Over 10 second slower.

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Mar 01, 2023 @ 11:45
    Dave Woestenborghs
    0

    Hi Mateusz,

    First of all you can get rid of ensuring that you have a UmbracoContext. As far as I can see you are not making use of it.

     public IContent GetContentById(int id)
        {
           var content = _contentService.GetById(id);
    
                return content;
        }
    

    Than I see that you wrap each publish action in a new scope. I have checked a import on large site and I see that I wrap all my publish actions in a single scope.

    So something like this :

    using (var scope = _scopeProvider.CreateScope())
            {
             // do all you import work here
    
    scope.Complete();
            }
    

    Dave

  • Mateusz Kotwica 29 posts 129 karma points
    Mar 01, 2023 @ 11:54
    Mateusz Kotwica
    0

    Thank you Dave for your advises, I will definitely try to implement them in my code.

    However, back to my case, we can treat my given example as a atomic/single use case. It's probably quite weird that the size of the content in the database affects the execution time of the code?

  • Mateusz Kotwica 29 posts 129 karma points
    Mar 01, 2023 @ 12:27
    Mateusz Kotwica
    0

    We can even stop the conversation about the code, as the issue is visible even from the Umbraco UI level.

    When Save and Publish is clicked on the particular document, there is significant difference in terms of how long the operation took:

    Small DB:

    small db

    Large DB:

    large db

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Mar 01, 2023 @ 12:39
    Dave Woestenborghs
    0

    HI Mateusz,

    Do you have any packages installed or do you hook into the published events of the content service ?

    Or maybe you have some custom indexing logic.

    Dave

  • Mateusz Kotwica 29 posts 129 karma points
    Mar 01, 2023 @ 13:10
    Mateusz Kotwica
    0

    I do not have any packages installed and I disabled all the event handlers I was subscribed to.

    I have an integration with elastic search, but the reindexing is triggered manually, so no automated hooks are implemented.

  • Mateusz Kotwica 29 posts 129 karma points
    Mar 01, 2023 @ 13:52
    Mateusz Kotwica
    0

    I replaced Umbrace.Core nuget package with Umbraco.Core project to be able to debug it.

    I added two timers: one to measure just a Save operation and second to measure entire method execution time:

    savepublish

    As you can see the CommitDocumentChangesInternal took only ~8 seconds (which still is way longer than executing entire metohd on small DB), but completing the scope took additional ~5seconds.

    Based on above findings the conclusion is: everything what is taking so much time is inside Umbraco code.

  • Marc Goodson 2155 posts 14406 karma points MVP 9x c-trib
    Mar 01, 2023 @ 18:57
    Marc Goodson
    0

    Hi Mateusz

    Every time you save and publish an Umbraco Content item it creates a version in the database.

    This enables editors to roll back to an earlier version.

    But if you are manipulating content items via code on a daily schedule these versions can build up and choke database performance...

    So if you go to one of these content items in back office and click rollback... Do you have a ton of versions available to roll back to?

    Or are you tidying up the versions after import?

    Apologies if you are already on the case with the version thing, just thought worth chipping in in case it was relevent for you in this context!

    Regards

    Marc

  • Mateusz Kotwica 29 posts 129 karma points
    Mar 01, 2023 @ 20:27
    Mateusz Kotwica
    0

    Hi Mark,

    thank you very much for your answer.

    I am aware of content versioning and we have clean up policies implemented. In addition that was my first thought: to clean up old versions and empty the recycle bin. Unfortunately it did not affect the save time at all.

  • Marc Goodson 2155 posts 14406 karma points MVP 9x c-trib
    Mar 01, 2023 @ 20:40
    Marc Goodson
    0

    Great Mateusz, that is good to hear you are on top of the versioning!

    Then I only have 9ne further thought, and appreciate its irritating if you have already thought this!

    But how is the % defragmentation of your various SQL indexes??

    The following gist gives a set of SQL statements that would give you a report

    https://gist.github.com/marcemarc/9d6553e881a7cf536c8bd98f8c35fd92

    And whether any need rebuilding or reorganising.

    Regards

    Marc

  • Mateusz Kotwica 29 posts 129 karma points
    Mar 01, 2023 @ 22:46
    Mateusz Kotwica
    0

    Thank you for this idea, I even was not aware about this.

    Here are the results for my smaller DB:

    smalldb

    And the results for the big one:

    largedb

    It looks like small db has worse fragmentation metrics but is way faster.

    I'd like only to mention that in my large database I have:

    • 7 445 360 rows in UmbracoPropertyDataTable
    • 206 216 rows in UmbracoNode table
  • Marc Goodson 2155 posts 14406 karma points MVP 9x c-trib
    Mar 02, 2023 @ 11:01
    Marc Goodson
    0

    Hi Mateusz

    Yes if this were a smoking gun, you'd see greater fragmentation on the production than on development!

    and is often the thing that explains the performance difference, particularly after an Umbraco Upgrade/migration...

    There is a good article here about indexing and query performance if you are interested:

    https://learn.microsoft.com/en-us/sql/relational-databases/indexes/reorganize-and-rebuild-indexes?view=sql-server-ver16#a-positive-side-effect-of-index-rebuild

    There is this little aside:

    https://learn.microsoft.com/en-us/sql/relational-databases/indexes/reorganize-and-rebuild-indexes?view=sql-server-ver16#a-positive-side-effect-of-index-rebuild

    so I guess rebuilding the index will also update statistics on your database columns, that could improve performance in production - or at the very least you 'rule it out'.

    https://learn.microsoft.com/en-us/sql/relational-databases/statistics/update-statistics?view=sql-server-ver16

    regards

    Marc

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Mar 02, 2023 @ 08:28
    Dave Woestenborghs
    0

    Hi Mateusz,

    Maybe rebuilding or reorganizing your indexes can result in better performance.

    Dave

  • Mateusz Kotwica 29 posts 129 karma points
    Mar 02, 2023 @ 10:54
    Mateusz Kotwica
    0

    Hi Dave,

    I've rebuilt/reorganized the indexes, with no luck - unfortunately..

    enter image description here

  • Mateusz Kotwica 29 posts 129 karma points
    Mar 02, 2023 @ 11:07
    Mateusz Kotwica
    0

    Thank you Marc, I will read it for sure.

    For today I plan to play a little bit with sql profiler to check if/where there are differences between databases.

    I'll post the findings.

  • Nicholas Westby 2054 posts 7103 karma points c-trib
    Jul 12, 2023 @ 14:37
    Nicholas Westby
    0

    Have you tried disabling your Examine indexing on publish (temporarily) to see if that might be the issue? https://our.umbraco.com/forum/using-umbraco-and-getting-started/109579-disabled-examine

Please Sign in or register to post replies

Write your reply to:

Draft