Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

model.COUNT() only firing once per action. (the same Column Statistic will only fire once per action?) #562

Closed
kmd1970 opened this issue Oct 28, 2015 · 6 comments
Labels

Comments

@kmd1970
Copy link

kmd1970 commented Oct 28, 2015

When I call this test action I expect to get 0 1 as the output from each model count(), however I always get 0 0.

When I check my debug, only one SQL count(*) is executed and and then two SQL inserts right after. Shouldn't all code be executed in the order written?

I am expecting one SQL count(*) statement right before each SQL insert to return the number of similar items inserted. I've tried adding save(transaction='commit') and using create(), but the results are the same. Confused?

<cffunction name="test" output="true">
 <cfscript> 
    var test = model("test").new();
    test.value = "test";
    test.parent_id = "99";
    var count = model("test").count(where="parent_id = '99'");
    writedump(count);

    test.save();
    //model("test").create(test);

    var test = model("test").new();
    test.value = "test";
    test.parent_id = "99";
    var count = model("test").count(where="parent_id = '99'");
    writedump(count);

    test.save();
    //model("test").create(test);
</cfscript>
</cffunction>

This results in only one SQL count(*) being executed, because the WHERE clause is the same.

<cffunction name="test" output="true">
       <cfscript> 
           var count = model("test").count(where="parent_id = '99'");
           var count = model("test").count(where="parent_id = '99'");
           var count = model("test").count(where="parent_id = '99'");
        </cfscript>
</cffunction>

My Table

CREATE TABLE [dbo].[TESTS](
    [id] [int] IDENTITY(1,1) NOT NULL,
    [uuid] [char](36) NOT NULL DEFAULT (newid()),
    [value] [char](36) NULL,
    [updatedat] [datetime] NULL,
    [updated_by] [varchar](36) NULL,
    [created_by] [varchar](36) NULL,
    [deletedat] [datetime] NULL,
    [createdat] [datetime] NULL,
    [parent_id] [char](36) NULL,
 CONSTRAINT [PK_TESTS] PRIMARY KEY CLUSTERED 
(
    [id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
)  ON [PRIMARY]

GO
@andybellenie
Copy link
Contributor

All model queries are cached for the duration of the request

To force a reload, add reload=true to the model call, i.e.
model("test").count(where="parent_id = '99'", reload=true);

On 28 October 2015 at 16:16, kmd1970 notifications@github.com wrote:

When I call this test action I expect to get 0 1 as the output from each
model count(), however I always get 0 0.

When I check my debug, only one SQL count(*) is executed and and then two
SQL inserts right after. Shouldn't all code be executed in the order
written?

I am expecting one SQL count(*) statement right before each SQL insert to
return the number of similar items inserted. I've tried adding
save(transaction='commit') and using create(), but the results are the
same. Confused?

var test = model("test").new(); test.value = "test"; test.parent_id = "99"; var count = model("test").count(where="parent_id = '99'"); writedump(count);
test.save();
//model("test").create(test);

var test = model("test").new();
test.value = "test";
test.parent_id = "99";
var count = model("test").count(where="parent_id = '99'");
writedump(count);

test.save();
//model("test").create(test);

This results in only one SQL count(*) being executed, because the WHERE
clause is the same.

var count = model("test").count(where="parent_id = '99'"); var count = model("test").count(where="parent_id = '99'"); var count = model("test").count(where="parent_id = '99'");

My Table

CREATE TABLE [dbo].[TESTS]([id] [int] IDENTITY%281,1%29 NOT NULL,
[uuid] [char]%2836%29 NOT NULL DEFAULT %28newid%28%29%29,
[value] [char]%2836%29 NULL,
[updatedat] [datetime] NULL,
[updated_by] [varchar]%2836%29 NULL,
[created_by] [varchar]%2836%29 NULL,
[deletedat] [datetime] NULL,
[createdat] [datetime] NULL,
[parent_id] [char]%2836%29 NULL,
CONSTRAINT [PK_TESTS] PRIMARY KEY CLUSTERED
%28
[id] ASC
%29WITH %28PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON%29 ON [PRIMARY]) ON [PRIMARY]

GO


Reply to this email directly or view it on GitHub
#562.

@kmd1970
Copy link
Author

kmd1970 commented Oct 28, 2015

Hi Andy, Thanks for the quick reply.

The reload parameter should be added to the count() documentation. I assumed reload=true was only to reload the object from the database once an insert/update has completed.

@kmd1970 kmd1970 closed this as completed Oct 28, 2015
@perdjurner
Copy link
Contributor

Reopening so we can document.

@perdjurner perdjurner reopened this Oct 28, 2015
@perdjurner perdjurner added docs and removed invalid labels Oct 28, 2015
@chrisdpeters
Copy link
Contributor

Got this documented.
http://docs.cfwheels.org/v1.4/docs/count

Also replaced the "See docs for findall" references with the appropriate descriptions.

@perdjurner
Copy link
Contributor

Great, thanks Chris.

@kmd1970
Copy link
Author

kmd1970 commented Oct 29, 2015

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants