Picking database columns for QQuery
By default QQuery selects all the columns from the table and thus populates all the properties of the resulting objects. Normally, this is the right thing to do - the most expensive part of a typical query is hitting the database and performing the query; once the query is performed, fetching as much data as possible is the most efficient behaviour.However, when some tables have a large amount of columns, or some LOB columns, this may become expensive, both in terms of the traffic generated between application and database, and in terms of the memory footprint of the application.
QQ::Select solves this problem by allowing to pick only the desired subset of columns to fetch from the database.
QQ::Select can be passed as a clause to any query method. As shown in the second example below, it can also be passed as an argument to QQ::Expand() to pick specific columns to fetch for the object to be expanded.
Note, that when QQ::Select is used, the primary keys are always automatically added to the select list. This is illustrated by the first example below, where QQN::Person()->Id is not part of the QQ::Select list, but $objPerson->Id is populated and used afterwards.
One QQ::Select() can be used to select multiple columns, as shown in the third example below:
QQ::Select(QQN::Person()->Address->Street, QQN::Person()->Address->City)
The examples also have some
assert(is_null(...)) calls, to show that the data for not selected columns is indeed not loaded.
You can also verify this by examining the performed queries in the profiling details.
You may also notice, that many times the QQ::Select clause is passed as the last argument to the query method. Even though this is not ideal (since in SQL the select clause is the first in a statement), it was necessary for backward compatibility reasons with older versions of QCubed.
Get the first names of all the people
1 John2 Kendall2
3 Ben
4 Mike
5 Alex
6 Wendy
7 Karen
8 Samantha
9 Linda
10 Jennifer
11 Brett
12 Jacob
13 sdf
14 sdhgfhsdg
15 billy
16 Nice
17 good
22 moi
27 test
29 sdfsdfsd
32 Fred
33 czx
34 aaaaaaa
35 Fred
37 sgsgsg
Get the last names of all the people, and the amount spent on the project they manage (if any), for Projects that have 'ACME' or 'HR' in it. Sort the result by Last Name, then First Name
Notice how some people may be listed twice, if they manage more than one project.Wolfe's project spent $5175.00
Wolfe's project spent $10251.00
Projects and Addresses for each Person
JohnAddresses: 1 Love Drive, Phoenix;
Projects where this person is a project manager:
started on Mar 01 2006 (milestones: Milestone F; )
Kendall2
Addresses: test, Dallas;
Projects where this person is a project manager: none
Ben
Addresses: 3 Gold Fish Pl., New York; 323 W QCubed, New York;
Projects where this person is a project manager: none
Mike
Addresses: none
Projects where this person is a project manager:
started on Feb 15 2006 (milestones: Milestone D; Milestone E; )
Alex
Addresses: 22 Elm St, Palo Alto1;
Projects where this person is a project manager: none
Wendy
Addresses: none
Projects where this person is a project manager: none
Karen
Addresses: 421 Central Expw, Mountain View3;
Projects where this person is a project manager:
started on Mar 01 2004 (milestones: Milestone A; Milestone B; Milestone C; )
started on Aug 15 2005 (milestones: Milestone G; Milestone H; Milestone I; Milestone J; )
Samantha
Addresses: none
Projects where this person is a project manager: none
Linda
Addresses: none
Projects where this person is a project manager: none
Jennifer
Addresses: none
Projects where this person is a project manager: none
Brett
Addresses: none
Projects where this person is a project manager: none
Jacob
Addresses: none
Projects where this person is a project manager: none
sdf
Addresses: none
Projects where this person is a project manager: none
sdhgfhsdg
Addresses: none
Projects where this person is a project manager: none
billy
Addresses: none
Projects where this person is a project manager: none
Nice
Addresses: none
Projects where this person is a project manager: none
good
Addresses: none
Projects where this person is a project manager: none
moi
Addresses: none
Projects where this person is a project manager: none
test
Addresses: none
Projects where this person is a project manager: none
sdfsdfsd
Addresses: none
Projects where this person is a project manager: none
Fred
Addresses: none
Projects where this person is a project manager: none
czx
Addresses: none
Projects where this person is a project manager: none
aaaaaaa
Addresses: none
Projects where this person is a project manager: none
Fred
Addresses: none
Projects where this person is a project manager: none
sgsgsg
Addresses: none
Projects where this person is a project manager: none
PROFILING INFORMATION FOR DATABASE CONNECTION #1: 4 queries performed. Please click here to view profiling detail
