Need help in tuning this select query which takes 2hrs
SQL Server 2012 EE SP2
Please let me know if you need any further details.
Edit: Updated text file with some comments.
If you need help with that, please provide the function definition and create statements for the tables and indexes. This is primarily a plan analysis site, but if the logic is clear, we might be able to suggest a way to rewrite it to avoid the horrible performance characteristics of non-inline functions that access the database.