· 8 years ago · May 19, 2018, 06:32 PM
1#!/usr/bin/env ruby
2#
3# A one file test to show count
4require 'rubygems'
5require 'dm-core'
6require 'dm-aggregates'
7
8# setup the logger
9DataMapper::Logger.new(STDOUT, :debug)
10
11# connect to the DB
12DataMapper.setup(:default, 'sqlite3::memory:')
13
14class Thing
15 include DataMapper::Resource
16
17 # properties
18 property :id, Serial
19
20 has n, :widgets
21end
22
23class Widget
24 include DataMapper::Resource
25
26 # properties
27 property :id, Serial
28
29 belongs_to :thing
30end
31
32
33DataMapper.auto_migrate!
34
3510.times do
36 thing = Thing.create
37 thing.widgets.create
38end
39
40@t = Thing.first
41
42puts "-"*80
43
44# modify each
45DataMapper::Collection.class_eval do
46 alias_method :orig_each, :each
47
48 def each(eager_load = true, &block)
49 eager_load ? orig_each(&block) : super(&block)
50 end
51end
52# --------
53
54Thing.all.each(false) do |thing|
55 puts thing.widgets.count
56end
57
58__END__
59
60 ~ (0.000180) SELECT sqlite_version(*)
61 ~ (0.000320) DROP TABLE IF EXISTS "things"
62 ~ (0.000032) DROP TABLE IF EXISTS "widgets"
63 ~ (0.000889) PRAGMA table_info("things")
64 ~ (0.000470) CREATE TABLE "things" ("id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT)
65 ~ (0.000031) PRAGMA table_info("widgets")
66 ~ (0.000191) CREATE TABLE "widgets" ("id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "thing_id" INTEGER NOT NULL)
67 ~ (0.000886) CREATE INDEX "index_widgets_thing" ON "widgets" ("thing_id")
68 ~ (0.000068) INSERT INTO "things" DEFAULT VALUES
69 ~ (0.000091) INSERT INTO "widgets" ("thing_id") VALUES (1)
70 ~ (0.000062) INSERT INTO "things" DEFAULT VALUES
71 ~ (0.000088) INSERT INTO "widgets" ("thing_id") VALUES (2)
72 ~ (0.000078) INSERT INTO "things" DEFAULT VALUES
73 ~ (0.000096) INSERT INTO "widgets" ("thing_id") VALUES (3)
74 ~ (0.000070) INSERT INTO "things" DEFAULT VALUES
75 ~ (0.000091) INSERT INTO "widgets" ("thing_id") VALUES (4)
76 ~ (0.000077) INSERT INTO "things" DEFAULT VALUES
77 ~ (0.000089) INSERT INTO "widgets" ("thing_id") VALUES (5)
78 ~ (0.000128) INSERT INTO "things" DEFAULT VALUES
79 ~ (0.000090) INSERT INTO "widgets" ("thing_id") VALUES (6)
80 ~ (0.000078) INSERT INTO "things" DEFAULT VALUES
81 ~ (0.000093) INSERT INTO "widgets" ("thing_id") VALUES (7)
82 ~ (0.000075) INSERT INTO "things" DEFAULT VALUES
83 ~ (0.000090) INSERT INTO "widgets" ("thing_id") VALUES (8)
84 ~ (0.000064) INSERT INTO "things" DEFAULT VALUES
85 ~ (0.000090) INSERT INTO "widgets" ("thing_id") VALUES (9)
86 ~ (0.000070) INSERT INTO "things" DEFAULT VALUES
87 ~ (0.000099) INSERT INTO "widgets" ("thing_id") VALUES (10)
88 ~ (0.000070) SELECT "id" FROM "things" ORDER BY "id" LIMIT 1
89--------------------------------------------------------------------------------
90 ~ (0.000070) SELECT "id" FROM "things" ORDER BY "id"
91 ~ (0.000067) SELECT COUNT(*) FROM "widgets" WHERE "thing_id" = 1
921
93 ~ (0.000069) SELECT COUNT(*) FROM "widgets" WHERE "thing_id" = 2
941
95 ~ (0.000062) SELECT COUNT(*) FROM "widgets" WHERE "thing_id" = 3
961
97 ~ (0.000063) SELECT COUNT(*) FROM "widgets" WHERE "thing_id" = 4
981
99 ~ (0.000064) SELECT COUNT(*) FROM "widgets" WHERE "thing_id" = 5
1001
101 ~ (0.000064) SELECT COUNT(*) FROM "widgets" WHERE "thing_id" = 6
1021
103 ~ (0.000062) SELECT COUNT(*) FROM "widgets" WHERE "thing_id" = 7
1041
105 ~ (0.000085) SELECT COUNT(*) FROM "widgets" WHERE "thing_id" = 8
1061
107 ~ (0.000074) SELECT COUNT(*) FROM "widgets" WHERE "thing_id" = 9
1081
109 ~ (0.000072) SELECT COUNT(*) FROM "widgets" WHERE "thing_id" = 10
1101