Skip to content

Commit d0c3756

Browse files
author
Yannig Perré
committed
Optimization for tablespaces request.
With previous version (under heavy load), the request can take more than 10 seconds. Fix another issue where the request send back nil result.
1 parent eed0d12 commit d0c3756

File tree

1 file changed

+70
-77
lines changed

1 file changed

+70
-77
lines changed

main.go

Lines changed: 70 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -259,86 +259,79 @@ func ScrapeTablespace(db *sql.DB, ch chan<- prometheus.Metric) error {
259259
rows *sql.Rows
260260
err error
261261
)
262-
rows, err = db.Query(`SELECT
263-
a.tablespace_name "Tablespace",
264-
b.status "Status",
265-
b.contents "Type",
266-
b.extent_management "Extent Mgmt",
267-
a.bytes bytes,
268-
a.maxbytes bytes_max,
269-
c.bytes_free + NVL(d.bytes_expired,0) bytes_free
270-
FROM
271-
(
272-
-- belegter und maximal verfuegbarer platz pro datafile
273-
-- nach tablespacenamen zusammengefasst
274-
-- => bytes
275-
-- => maxbytes
276-
SELECT
277-
a.tablespace_name,
278-
SUM(a.bytes) bytes,
279-
SUM(DECODE(a.autoextensible, 'YES', a.maxbytes, 'NO', a.bytes)) maxbytes
280-
FROM
281-
dba_data_files a
282-
GROUP BY
283-
tablespace_name
284-
) a,
285-
sys.dba_tablespaces b,
286-
(
287-
-- freier platz pro tablespace
288-
-- => bytes_free
289-
SELECT
290-
a.tablespace_name,
291-
SUM(a.bytes) bytes_free
292-
FROM
293-
dba_free_space a
294-
GROUP BY
295-
tablespace_name
296-
) c,
297-
(
298-
-- freier platz durch expired extents
299-
-- speziell fuer undo tablespaces
300-
-- => bytes_expired
301-
SELECT
302-
a.tablespace_name,
303-
SUM(a.bytes) bytes_expired
304-
FROM
305-
dba_undo_extents a
306-
WHERE
307-
status = 'EXPIRED'
308-
GROUP BY
309-
tablespace_name
310-
) d
311-
WHERE
312-
a.tablespace_name = c.tablespace_name (+)
313-
AND a.tablespace_name = b.tablespace_name
314-
AND a.tablespace_name = d.tablespace_name (+)
315-
UNION ALL
262+
rows, err = db.Query(`
316263
SELECT
317-
d.tablespace_name "Tablespace",
318-
b.status "Status",
319-
b.contents "Type",
320-
b.extent_management "Extent Mgmt",
321-
sum(a.bytes_free + a.bytes_used) bytes, -- allocated
322-
SUM(DECODE(d.autoextensible, 'YES', d.maxbytes, 'NO', d.bytes)) bytes_max,
323-
SUM(a.bytes_free + a.bytes_used - NVL(c.bytes_used, 0)) bytes_free
264+
Z.name,
265+
dt.status,
266+
dt.contents,
267+
dt.extent_management,
268+
Z.bytes,
269+
Z.max_bytes,
270+
Z.free_bytes
324271
FROM
325-
sys.v_$TEMP_SPACE_HEADER a,
326-
sys.dba_tablespaces b,
327-
sys.v_$Temp_extent_pool c,
328-
dba_temp_files d
272+
(
273+
SELECT
274+
X.name as name,
275+
SUM(nvl(X.free_bytes,0)) as free_bytes,
276+
SUM(X.bytes) as bytes,
277+
SUM(X.max_bytes) as max_bytes
278+
FROM
279+
(
280+
SELECT
281+
ddf.tablespace_name as name,
282+
ddf.status as status,
283+
ddf.bytes as bytes,
284+
sum(dfs.bytes) as free_bytes,
285+
CASE
286+
WHEN ddf.maxbytes = 0 THEN ddf.bytes
287+
ELSE ddf.maxbytes
288+
END as max_bytes
289+
FROM
290+
sys.dba_data_files ddf,
291+
sys.dba_tablespaces dt,
292+
sys.dba_free_space dfs
293+
WHERE ddf.tablespace_name = dt.tablespace_name
294+
AND ddf.file_id = dfs.file_id(+)
295+
GROUP BY
296+
ddf.tablespace_name,
297+
ddf.file_name,
298+
ddf.status,
299+
ddf.bytes,
300+
ddf.maxbytes
301+
) X
302+
GROUP BY X.name
303+
UNION ALL
304+
SELECT
305+
Y.name as name,
306+
MAX(nvl(Y.free_bytes,0)) as free_bytes,
307+
SUM(Y.bytes) as bytes,
308+
SUM(Y.max_bytes) as max_bytes
309+
FROM
310+
(
311+
SELECT
312+
dtf.tablespace_name as name,
313+
dtf.status as status,
314+
dtf.bytes as bytes,
315+
(
316+
SELECT
317+
((f.total_blocks - s.tot_used_blocks)*vp.value)
318+
FROM
319+
(SELECT tablespace_name, sum(used_blocks) tot_used_blocks FROM gv$sort_segment WHERE tablespace_name!='DUMMY' GROUP BY tablespace_name) s,
320+
(SELECT tablespace_name, sum(blocks) total_blocks FROM dba_temp_files where tablespace_name !='DUMMY' GROUP BY tablespace_name) f,
321+
(SELECT value FROM v$parameter WHERE name = 'db_block_size') vp
322+
WHERE f.tablespace_name=s.tablespace_name AND f.tablespace_name = dtf.tablespace_name
323+
) as free_bytes,
324+
CASE
325+
WHEN dtf.maxbytes = 0 THEN dtf.bytes
326+
ELSE dtf.maxbytes
327+
END as max_bytes
328+
FROM
329+
sys.dba_temp_files dtf
330+
) Y
331+
GROUP BY Y.name
332+
) Z, sys.dba_tablespaces dt
329333
WHERE
330-
c.file_id(+) = a.file_id
331-
and c.tablespace_name(+) = a.tablespace_name
332-
and d.file_id = a.file_id
333-
and d.tablespace_name = a.tablespace_name
334-
and b.tablespace_name = a.tablespace_name
335-
GROUP BY
336-
b.status,
337-
b.contents,
338-
b.extent_management,
339-
d.tablespace_name
340-
ORDER BY
341-
1
334+
Z.name = dt.tablespace_name
342335
`)
343336
if err != nil {
344337
return err

0 commit comments

Comments
 (0)