General background

The Link database saves all links between individual items across the databases (core, master, web) and language versions. From this database, it is possible to find out a list of items, which the specific items refers to but also to items, which refer to this item, for example, a page create from a specific template contains a reference to this particular template. The existence of this database is primarily important for increasing the speed in detecting such information. In content editor, this database is for example used in “Navigate -> Links“.

Principle

The link database as such is saved in the LINKS table. The process of recording into this table is based on events, which are triggered during actions with items.
More specifically, it deals with the following events:

  • item:copied
  • item:deleted
  • item:saved
  • item:versionRemoved

When triggering any of the above events, a record into the link database takes place. When triggering the item: deleted, you even have the possibility to choose what is to be done with the existing links on the deleted item. This is a dialog window, which is triggered during the deleting of any item in Sitecore (see Figure).

delete-link

If the user chooses the “Leave links“ option, the update in the link database is not carried out properly and this leads to inconsistency. An item, which referred to the deleted item, will now refer to a non-existing item. Therefore, unless the user has a specific reason, he should always let all the links for the deleted item be deleted by choosing the “Remove links“ options. Due to a possible inconsistency of the link database, it is necessary to update the link database every once in awhile. The following chapter describes the method of carrying out this update.

Administration

Due to inconsistencies of the link database, we sometimes need to update the database. In relation to the number of entities in Sitecore and the number of language versions, this operation is a very time consuming operation. Therefore, it is suitable to carry out such update during the night, when the system is not so overloaded. For larger webs, the total update time can range in hours. It is possible to run this operation manually in “Control Panel -> Database -> Rebuild the Link Database“, see Figure.

rebuild-link-db

We can choose which database we want to update in the dialog window. We must often need to update the master database, where the most number of changes take place. If updating the link database takes too long, it is convenient to run such operations for only one database at once. .
If it is necessary to update the link database regularly, it is possible to write a job, which the rebuild database will carry out automatically at the planned time. If the rebuilding of one database is considerably time consuming, it is possible to modify the job in such a way so that it only regularly carries the rebuilding out in the defined parts of the tree, specific items, and so on. It is possible to carry out the rebuilding link database for specific items using the following code: (rebuild /sitecore/content/Home):

   var dbName = "master"; 
   var db = Factory.GetDatabase(dbName);
   var item = db.GetItem("/sitecore/content/Home"); 
   Globals.LinkDatabase.UpdateReferences(item);

Possible Methods of Configuration

Using the configuration, it is possible to set up into which database the link database will be saved. Sitecore’s default for saving is set up as the core database. It is possible to change this setting in relation to the infrastructure of the specific project. The setting are found in the configuration file sitecore.config:

<LinkDatabase type="Sitecore.Data.$(database).$(database)LinkDatabase, Sitecore.Kernel">
      <param connectionStringName="core"/>
</LinkDatabase>

The possible patch for changing the database can be as follows:

<LinkDatabase type="Sitecore.Data.$(database).$(database)LinkDatabase, Sitecore.Kernel">
          <param connectionStringName="web" patch:instead="param[@connectionStringName='core']" />
</LinkDatabase>

If content management (CM) and content delivery (CD) are separate, it is suitable to consider transferring the link database for CD to the db web. This leads to a larger CM and CD separation, which is very beneficial.

If we have the possibility to significantly increase the output of only one database, for example, master database, which is on Azure, it is worth considering transferring the link database for CM into the master db instead of the core db.

Examples of Work with the Link Database

Rebuild the Link Database:

   var dbName = "master"; 
   var db = Factory.GetDatabase(dbName);
   Globals.LinkDatabase.Rebuild(db);

Upload all references to /sitecore/content/Home:

   var dbName = "master"; 
   var db = Factory.GetDatabase(dbName);
   var item = db.GetItem("/sitecore/content/Home"); 
   ItemLink[] links = Globals.LinkDatabase.GetReferrers(item);