SQL Function, get max dates from 2 tables then compare with given paramter to function

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
0
down vote

favorite












ALTER FUNCTION [dbo].[GetLastModifiedDate](@ProductURL nvarchar(255), @ProductLastModified datetime)
RETURNS datetime
AS
BEGIN

DECLARE @Result datetime;
DECLARE @NotesLastMod datetime;
DECLARE @DealsLastMod datetime;

SELECT @NotesLastMod = MAX(LastModified) FROM Notes WHERE ListURL LIKE @ProductURL + '%';
SELECT @DealsLastMod = MAX(DealLastModified) FROM Deals WHERE ListURL LIKE @ProductURL + '%';

SELECT @Result = MAX(LastMod) FROM (VALUES(@ProductLastModified),(@NotesLastMod),(@DealsLastMod)) T(LastMod);
RETURN @Result
END


A very simple self explaining function, gets latest dates from two tables and compare it with given datetime.



Usage is like this,



// SQL Query 1
// SQL Query 2
SELECT dbo.LastModified(Products.URL, Products.LastModified) AS LastModified
FROM PRODUCTS
// Some Inner Joins
// Some Where clauses


Now this function takes around 41 seconds if Notes and Deals tables have 25000 and 300000 rows respectively. We are using SQL Server 2012







share|improve this question



















  • You need index on ListURL fields of both tables
    – Eralper
    Feb 7 at 15:07
















up vote
0
down vote

favorite












ALTER FUNCTION [dbo].[GetLastModifiedDate](@ProductURL nvarchar(255), @ProductLastModified datetime)
RETURNS datetime
AS
BEGIN

DECLARE @Result datetime;
DECLARE @NotesLastMod datetime;
DECLARE @DealsLastMod datetime;

SELECT @NotesLastMod = MAX(LastModified) FROM Notes WHERE ListURL LIKE @ProductURL + '%';
SELECT @DealsLastMod = MAX(DealLastModified) FROM Deals WHERE ListURL LIKE @ProductURL + '%';

SELECT @Result = MAX(LastMod) FROM (VALUES(@ProductLastModified),(@NotesLastMod),(@DealsLastMod)) T(LastMod);
RETURN @Result
END


A very simple self explaining function, gets latest dates from two tables and compare it with given datetime.



Usage is like this,



// SQL Query 1
// SQL Query 2
SELECT dbo.LastModified(Products.URL, Products.LastModified) AS LastModified
FROM PRODUCTS
// Some Inner Joins
// Some Where clauses


Now this function takes around 41 seconds if Notes and Deals tables have 25000 and 300000 rows respectively. We are using SQL Server 2012







share|improve this question



















  • You need index on ListURL fields of both tables
    – Eralper
    Feb 7 at 15:07












up vote
0
down vote

favorite









up vote
0
down vote

favorite











ALTER FUNCTION [dbo].[GetLastModifiedDate](@ProductURL nvarchar(255), @ProductLastModified datetime)
RETURNS datetime
AS
BEGIN

DECLARE @Result datetime;
DECLARE @NotesLastMod datetime;
DECLARE @DealsLastMod datetime;

SELECT @NotesLastMod = MAX(LastModified) FROM Notes WHERE ListURL LIKE @ProductURL + '%';
SELECT @DealsLastMod = MAX(DealLastModified) FROM Deals WHERE ListURL LIKE @ProductURL + '%';

SELECT @Result = MAX(LastMod) FROM (VALUES(@ProductLastModified),(@NotesLastMod),(@DealsLastMod)) T(LastMod);
RETURN @Result
END


A very simple self explaining function, gets latest dates from two tables and compare it with given datetime.



Usage is like this,



// SQL Query 1
// SQL Query 2
SELECT dbo.LastModified(Products.URL, Products.LastModified) AS LastModified
FROM PRODUCTS
// Some Inner Joins
// Some Where clauses


Now this function takes around 41 seconds if Notes and Deals tables have 25000 and 300000 rows respectively. We are using SQL Server 2012







share|improve this question











ALTER FUNCTION [dbo].[GetLastModifiedDate](@ProductURL nvarchar(255), @ProductLastModified datetime)
RETURNS datetime
AS
BEGIN

DECLARE @Result datetime;
DECLARE @NotesLastMod datetime;
DECLARE @DealsLastMod datetime;

SELECT @NotesLastMod = MAX(LastModified) FROM Notes WHERE ListURL LIKE @ProductURL + '%';
SELECT @DealsLastMod = MAX(DealLastModified) FROM Deals WHERE ListURL LIKE @ProductURL + '%';

SELECT @Result = MAX(LastMod) FROM (VALUES(@ProductLastModified),(@NotesLastMod),(@DealsLastMod)) T(LastMod);
RETURN @Result
END


A very simple self explaining function, gets latest dates from two tables and compare it with given datetime.



Usage is like this,



// SQL Query 1
// SQL Query 2
SELECT dbo.LastModified(Products.URL, Products.LastModified) AS LastModified
FROM PRODUCTS
// Some Inner Joins
// Some Where clauses


Now this function takes around 41 seconds if Notes and Deals tables have 25000 and 300000 rows respectively. We are using SQL Server 2012









share|improve this question










share|improve this question




share|improve this question









asked Feb 6 at 8:44









Mathematics

3581521




3581521











  • You need index on ListURL fields of both tables
    – Eralper
    Feb 7 at 15:07
















  • You need index on ListURL fields of both tables
    – Eralper
    Feb 7 at 15:07















You need index on ListURL fields of both tables
– Eralper
Feb 7 at 15:07




You need index on ListURL fields of both tables
– Eralper
Feb 7 at 15:07















active

oldest

votes











Your Answer




StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");

StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f186893%2fsql-function-get-max-dates-from-2-tables-then-compare-with-given-paramter-to-fu%23new-answer', 'question_page');

);

Post as a guest



































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes










 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f186893%2fsql-function-get-max-dates-from-2-tables-then-compare-with-given-paramter-to-fu%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

Greedy Best First Search implementation in Rust

Function to Return a JSON Like Objects Using VBA Collections and Arrays

C++11 CLH Lock Implementation