You can find a step-by-step guide on how to rebuild your database here.
This process isn't difficult but it will take time to complete so begin the process when you have plans or something else to do which doesn't require your PS4. If any issues are found during the scan, the offending files will be replaced. In the process, it will find compromised data from a game or app that might be causing added strain on the system. This will scan the hard drive of your PS4 and create a new database of all the games and apps on your system. If the easy fixes haven't helped clean up your system lag the next step is rebuilding your PS4's database.