Question

Context:

Say I have a large query (pseudo):

SELECT %boundthing,data
WHERE data in (
    ...do lots of long-running stuff
)

Let's assume boundthing isn't being used in my query anywhere but when I want to print the output (I know it's a stupid use case, but it illustrates the point that I know boundthing won't be needed until after the WHERE clause runs).

As it is, if I pass the value for boundthing to my query in the programming language of my choice, it's evaluated when I fire the query off, regardless of how much later on it will actually be "used" by the database.

Question:

Is there a way (in an existing database/programming language) to set a bind-style thing to be evaluated (that is, it produces a value for the bound variable, whether the source is a variable or a function) on-demand in a query, rather than when the query plan is built?

For example, I could say, in application pseudocode:

function MyFunc:
    return "foo" + system.GetEpochTimestamp();

var results = database_handle.Execute( SQL = "
    SELECT %boundthing,data
    WHERE data in (
        ...do lots of long-running stuff
    )", BINDS = [ ("boundthing", MyFunc ) ]
);

And the value of "boundthing" in the output would correspond to the timestamp on the application server, after the WHERE clause filtered everything (minus the runtime of myfunc on the application server, and however long it took for the data to make it to the database server and be processed by the database).

Why I want to know:

Curiosity.

I know that something like this would probably destroy a lot (maybe all) of the efficiency lent by a query planner. I know that you would never have a guarantee that the evaluation of the bind would be executed exactly when the value was needed in the query (there's value transmission time, processing time on the database, etc). I also know that it violates a lot of atomicity guarantees and could cause serious problems in the event of an abort or outage.

Still, I'm curious.

Was it helpful?

Solution

CREATE PROC MyFunc
AS
BEGIN
    SELECT * INTO #MyTempTable FROM data where data in ( ...do lots of long-running stuff);
    $boundthing='EXEC boundthing'
    select $boundthing, * from #MyTempTable
END
GO

Hope that helps Zac.

-Pete

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top