Skip to content

Commit ff56239

Browse files
committed
Fix #46 - Added new rule G-3145: Avoid using SELECT \* directly from a table or view.
1 parent fa51433 commit ff56239

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# G-3145: Avoid using SELECT \* directly from a table or view.
2+
3+
!!! warning "Major"
4+
Efficiency, Maintainability, Reliability, Testability
5+
6+
## Reason
7+
8+
Use of SELECT \* when querying a table or view makes it impossible for the optimizer to take into account which columns will actually be used by the application, potentially leading to sub-optimal execution plans (for example full scanning the table where a full scan of an index might have sufficed.) Also SELECT \* possibly can break your code in the future in case of changes to the table structure (for example new or invisible columns.)
9+
10+
Exceptions to the rule can be when querying an inline view (where the SELECT \* is just to avoid repeating same columns as inside the inline view), or when fetching into records defined as MYTABLE%ROWTYPE for the purpose of processing all columns of the record.
11+
12+
## Example (bad)
13+
14+
``` sql
15+
begin
16+
for r_employee in (
17+
select *
18+
from employees
19+
)
20+
loop
21+
employee_api.calculate_raise_by_seniority(
22+
id_in => r_employee.id
23+
,salary_in => r_employee.salary
24+
,hiredate_in => r_employee.hiredate
25+
);
26+
end loop;
27+
end;
28+
/
29+
```
30+
31+
## Example (good)
32+
33+
``` sql
34+
begin
35+
for r_employee in (
36+
select id,salary,hiredate
37+
from employees
38+
)
39+
loop
40+
employee_api.calculate_raise_by_seniority(
41+
id_in => r_employee.id
42+
,salary_in => r_employee.salary
43+
,hiredate_in => r_employee.hiredate
44+
);
45+
end loop;
46+
end;
47+
/
48+
```

docs/9-appendix/appendix.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ n/a | 3115 | Avoid self-assigning a column. | Minor | | | ✘ | | | |
4949
27 | 3120 | Always use table aliases when your SQL statement involves more than one source. | Major | | | ✘ | | | | |
5050
28 | 3130 | Try to use ANSI SQL-92 join syntax. | Minor | | | ✘ | ✘ | | | |
5151
29 | 3140 | Try to use anchored records as targets for your cursors. | Major | | | ✘ | | ✘ | | |
52+
n/a | 3145 | Avoid using SELECT \* directly from a table or view. | Major | | ✘ | ✘ | | ✘ | | | ✘
5253
n/a | 3150 | Try to use identity columns for surrogate keys. | Minor | | | ✘ | | ✘ | | |
5354
n/a | 3160 | Avoid visible virtual columns. | Major | | | ✘ | | ✘ | | |
5455
n/a | 3170 | Always use DEFAULT ON NULL declarations to assign default values to table columns if you refuse to store NULL values. | Major | | | | | ✘ | | |

0 commit comments

Comments
 (0)