From fe89dac24ccc43d4a624db75b1e00a5c3b5b7a1b Mon Sep 17 00:00:00 2001 From: MattioliLeo Date: Tue, 2 Jul 2024 11:45:00 +0200 Subject: [PATCH] MS SQL Optimization --- .../support/alfrescodb/DAOMapper.java | 62 +++++++------- .../beans/ActivitiesFeedByApplication.java | 8 +- .../beans/ActivitiesFeedByTypeBean.java | 7 +- .../beans/ActivitiesFeedByUserBean.java | 7 +- .../support/alfrescodb/beans/DbMSSQLBean.java | 80 ------------------- .../alfrescodb/beans/DbMsSQLIndexBean.java | 47 +++++++++++ .../alfrescodb/beans/DbMsSQLTableBean.java | 47 +++++++++++ .../alfrescodb/controllers/WebController.java | 19 ++++- .../alfrescodb/export/ExportComponent.java | 15 +++- src/main/resources/templates/dbSize.html | 40 +++++----- 10 files changed, 181 insertions(+), 151 deletions(-) delete mode 100644 src/main/java/com/alfresco/support/alfrescodb/beans/DbMSSQLBean.java create mode 100644 src/main/java/com/alfresco/support/alfrescodb/beans/DbMsSQLIndexBean.java create mode 100644 src/main/java/com/alfresco/support/alfrescodb/beans/DbMsSQLTableBean.java diff --git a/src/main/java/com/alfresco/support/alfrescodb/DAOMapper.java b/src/main/java/com/alfresco/support/alfrescodb/DAOMapper.java index 0838b67..90fb048 100644 --- a/src/main/java/com/alfresco/support/alfrescodb/DAOMapper.java +++ b/src/main/java/com/alfresco/support/alfrescodb/DAOMapper.java @@ -13,7 +13,8 @@ import com.alfresco.support.alfrescodb.beans.AppliedPatchesBean; import com.alfresco.support.alfrescodb.beans.ArchivedNodesBean; import com.alfresco.support.alfrescodb.beans.ContentModelBean; -import com.alfresco.support.alfrescodb.beans.DbMSSQLBean; +import com.alfresco.support.alfrescodb.beans.DbMsSQLIndexBean; +import com.alfresco.support.alfrescodb.beans.DbMsSQLTableBean; import com.alfresco.support.alfrescodb.beans.DbMySQLBean; import com.alfresco.support.alfrescodb.beans.DbOracleBean; import com.alfresco.support.alfrescodb.beans.DbPostgresBean; @@ -75,37 +76,25 @@ public interface DAOMapper { "group by u.segment_name, i.table_name") List findIndexesInfoOracle(); - // MS SQL Queries - @Select("SELECT \n" + - " s.Name as SchemaName, t.NAME AS TableName, p.rows AS RowCounts,\n" + - " (SUM(a.total_pages) * 8 * 1024) AS TotalSpace, \n" + - " (SUM(a.used_pages) * 8 * 1024) AS UsedSpace, \n" + - " ((SUM(a.total_pages) - SUM(a.used_pages)) * 8 *1024 ) AS UnusedSpace \n" + - "FROM sys.tables t \n" + - "INNER JOIN sys.schemas s ON s.schema_id = t.schema_id \n" + - "INNER JOIN sys.indexes i ON t.OBJECT_ID = i.object_id \n" + - "INNER JOIN sys.partitions p ON i.object_id = p.OBJECT_ID AND i.index_id = p.index_id \n" + - "INNER JOIN sys.allocation_units a ON p.partition_id = a.container_id \n" + - "WHERE \n" + - " t.NAME NOT LIKE 'dt%' -- filter out system tables for diagramming \n" + - " AND t.is_ms_shipped = 0 \n" + - " AND i.OBJECT_ID > 255 \n" + - "GROUP BY \n" + - " t.Name, s.Name, p.Rows") - List findTablesInfoMSSql(); - - @Select("SELECT\n" + - " s.Name as SchemaName, OBJECT_NAME(i.OBJECT_ID) AS TableName,\n" + - " i.name AS IndexName,\n" + - " i.index_id AS IndexID,\n" + - " (8 * SUM(a.used_pages) * 1024) AS 'IndexSize'\n" + - "FROM sys.indexes AS i\n" + - "JOIN sys.partitions AS p ON p.OBJECT_ID = i.OBJECT_ID AND p.index_id = i.index_id \n" + - "JOIN sys.allocation_units AS a ON a.container_id = p.partition_id \n" + - "JOIN sys.tables t ON t.OBJECT_ID = i.object_id \n" + - "JOIN sys.schemas s ON s.schema_id = t.schema_id \n" + - "GROUP BY i.OBJECT_ID,i.index_id,i.name") - List findIndexesInfoMSSql(); + // MS SQL Queries - table stats are from first index, usually the PK + @Select("SELECT OBJECT_NAME(t.object_id) AS tableName, SUM(u.total_pages) * 8 * 1024 AS totalReservedBytes, " + + "SUM(u.used_pages) * 8 * 1024 AS usedSpaceBytes, MAX(p.rows) AS rowsCount, STATS_DATE(p.object_id,s.stats_id) AS statisticsUpdateDate " + + "FROM sys.allocation_units AS u " + + "JOIN sys.partitions AS p ON u.container_id = p.hobt_id " + + "JOIN sys.tables AS t ON p.object_id = t.object_id " + + "JOIN sys.stats AS s ON p.object_id = s.object_id and s.stats_id = 1 " + + "GROUP BY OBJECT_NAME(t.object_id), STATS_DATE(p.object_id,s.stats_id) ") + List findTablesInfoMSSql(); + + @Select("SELECT OBJECT_NAME(i.OBJECT_ID) AS tableName, " + + "i.name AS indexName, i.index_id AS indexID, 8 * 1024 * SUM(a.used_pages) AS indexSizeBytes, " + + "STATS_DATE(p.object_id,i.index_id) AS statisticsUpdateDate " + + "FROM sys.indexes i " + + "JOIN sys.partitions p ON p.OBJECT_ID = i.OBJECT_ID AND p.index_id = i.index_id " + + "JOIN sys.allocation_units a ON a.container_id = p.partition_id " + + "GROUP BY i.OBJECT_ID,i.index_id,i.[name],STATS_DATE(p.object_id,i.index_id) " + + "ORDER BY OBJECT_NAME(i.OBJECT_ID),i.index_id ") + List findIndexesInfoMSSql(); /* * Large Folders @@ -210,14 +199,14 @@ public interface DAOMapper { /* * Activities Feed */ - @Select("select count(*) as count, CAST(post_date AS DATE) postDate, site_network as siteNetwork, activity_type as activityType " + @Select("select count(*) as count, TRIM(cast(CAST(post_date AS date) as char)) postDate, site_network as siteNetwork, activity_type as activityType " + "from alf_activity_feed " + "where feed_user_id = post_user_id " + "group by CAST(post_date AS DATE), site_network, activity_type ") List listActivitiesByActivityType(); - @Select("select count(*) as count, CAST(post_date AS DATE) postDate, site_network as siteNetwork, feed_user_id as feedUserId " + @Select("select count(*) as count, TRIM(cast(CAST(post_date AS date) as char)) postDate, site_network as siteNetwork, feed_user_id as feedUserId " + "from alf_activity_feed " + "where feed_user_id != '@@NULL@@' " + @@ -225,7 +214,7 @@ public interface DAOMapper { "group by CAST(post_date AS DATE), site_network, feed_user_id ") List listActivitiesByUser(); - @Select("select count(*) as count, CAST(post_date AS DATE) postDate, site_network as siteNetwork, app_tool as appTool " + @Select("select count(*) as count, TRIM(cast(CAST(post_date AS date) as char)) postDate, site_network as siteNetwork, app_tool as appTool " + "from alf_activity_feed " + "where feed_user_id != '@@NULL@@' " + @@ -269,6 +258,9 @@ public interface DAOMapper { @Select("select count(*) as count from alf_auth_status where authorized is TRUE") String countAuthorizedUsers(); + @Select("select count(*) as count from alf_auth_status where authorized = 1") + String countAuthorizedUsersMicrosoft(); + @Select("select count(*) as count from alf_node_properties where qname_id in (select id from alf_qname where local_name = 'authorityName')") String countGroups(); diff --git a/src/main/java/com/alfresco/support/alfrescodb/beans/ActivitiesFeedByApplication.java b/src/main/java/com/alfresco/support/alfrescodb/beans/ActivitiesFeedByApplication.java index b310768..712af13 100644 --- a/src/main/java/com/alfresco/support/alfrescodb/beans/ActivitiesFeedByApplication.java +++ b/src/main/java/com/alfresco/support/alfrescodb/beans/ActivitiesFeedByApplication.java @@ -2,11 +2,10 @@ import java.io.Serializable; import java.math.BigInteger; -import java.sql.Date; public class ActivitiesFeedByApplication implements Serializable { private BigInteger count; - private Date postDate; + private String postDate; private String siteNetwork; private String appTool; @@ -16,10 +15,10 @@ public BigInteger getCount() { public void setCount(BigInteger count) { this.count = count; } - public Date getPostDate() { + public String getPostDate() { return postDate; } - public void setPostDate(Date postDate) { + public void setPostDate(String postDate) { this.postDate = postDate; } public String getSiteNetwork() { @@ -34,5 +33,4 @@ public String getAppTool() { public void setAppTool(String appTool) { this.appTool = appTool; } - } diff --git a/src/main/java/com/alfresco/support/alfrescodb/beans/ActivitiesFeedByTypeBean.java b/src/main/java/com/alfresco/support/alfrescodb/beans/ActivitiesFeedByTypeBean.java index e50e19e..787fe2b 100644 --- a/src/main/java/com/alfresco/support/alfrescodb/beans/ActivitiesFeedByTypeBean.java +++ b/src/main/java/com/alfresco/support/alfrescodb/beans/ActivitiesFeedByTypeBean.java @@ -2,11 +2,10 @@ import java.io.Serializable; import java.math.BigInteger; -import java.sql.Date; public class ActivitiesFeedByTypeBean implements Serializable { private BigInteger count; - private Date postDate; + private String postDate; private String siteNetwork; private String activityType; @@ -16,10 +15,10 @@ public BigInteger getCount() { public void setCount(BigInteger count) { this.count = count; } - public Date getPostDate() { + public String getPostDate() { return postDate; } - public void setPostDate(Date postDate) { + public void setPostDate(String postDate) { this.postDate = postDate; } public String getSiteNetwork() { diff --git a/src/main/java/com/alfresco/support/alfrescodb/beans/ActivitiesFeedByUserBean.java b/src/main/java/com/alfresco/support/alfrescodb/beans/ActivitiesFeedByUserBean.java index c2127fd..35f431a 100644 --- a/src/main/java/com/alfresco/support/alfrescodb/beans/ActivitiesFeedByUserBean.java +++ b/src/main/java/com/alfresco/support/alfrescodb/beans/ActivitiesFeedByUserBean.java @@ -2,11 +2,10 @@ import java.io.Serializable; import java.math.BigInteger; -import java.sql.Date; public class ActivitiesFeedByUserBean implements Serializable { private BigInteger count; - private Date postDate; + private String postDate; private String siteNetwork; private String feedUserId; @@ -16,10 +15,10 @@ public BigInteger getCount() { public void setCount(BigInteger occurrences) { this.count = occurrences; } - public Date getPostDate() { + public String getPostDate() { return postDate; } - public void setPostDate(Date post_date) { + public void setPostDate(String post_date) { this.postDate = post_date; } public String getSiteNetwork() { diff --git a/src/main/java/com/alfresco/support/alfrescodb/beans/DbMSSQLBean.java b/src/main/java/com/alfresco/support/alfrescodb/beans/DbMSSQLBean.java deleted file mode 100644 index 2980e60..0000000 --- a/src/main/java/com/alfresco/support/alfrescodb/beans/DbMSSQLBean.java +++ /dev/null @@ -1,80 +0,0 @@ -package com.alfresco.support.alfrescodb.beans; - -import java.io.Serializable; -import java.math.BigInteger; -import java.sql.Timestamp; - - -public class DbMSSQLBean implements Serializable{ - private String schemaname; - private String tablename; - private String rowestimates; - private BigInteger table_size; - private String pretty_size; - private BigInteger index_bytes; - private Timestamp last_vacuum; - private Timestamp last_autovacuum; - private Timestamp last_analyze; - private Timestamp last_autoanalyze; - - public String getSchemaname() { - return schemaname; - } - public void setSchemaname(String schemaname) { - this.schemaname = schemaname; - } - public String getTablename() { - return tablename; - } - public void setTablename(String tablename) { - this.tablename = tablename; - } - public String getRowestimates() { - return rowestimates; - } - public void setRowestimates(String rowestimates) { - this.rowestimates = rowestimates; - } - public BigInteger getTable_size() { - return table_size; - } - public void setTable_size(BigInteger table_size) { - this.table_size = table_size; - } - public String getPretty_size() { - return pretty_size; - } - public void setPretty_size(String pretty_size) { - this.pretty_size = pretty_size; - } - public BigInteger getIndex_bytes() { - return index_bytes; - } - public void setIndex_bytes(BigInteger index_bytes) { - this.index_bytes = index_bytes; - } - public Timestamp getLast_vacuum() { - return last_vacuum; - } - public void setLast_vacuum(Timestamp last_vacuum) { - this.last_vacuum = last_vacuum; - } - public Timestamp getLast_autovacuum() { - return last_autovacuum; - } - public void setLast_autovacuum(Timestamp last_autovacuum) { - this.last_autovacuum = last_autovacuum; - } - public Timestamp getLast_analyze() { - return last_analyze; - } - public void setLast_analyze(Timestamp last_analyze) { - this.last_analyze = last_analyze; - } - public Timestamp getLast_autoanalyze() { - return last_autoanalyze; - } - public void setLast_autoanalyze(Timestamp last_autoanalyze) { - this.last_autoanalyze = last_autoanalyze; - } -} diff --git a/src/main/java/com/alfresco/support/alfrescodb/beans/DbMsSQLIndexBean.java b/src/main/java/com/alfresco/support/alfrescodb/beans/DbMsSQLIndexBean.java new file mode 100644 index 0000000..f7b9e50 --- /dev/null +++ b/src/main/java/com/alfresco/support/alfrescodb/beans/DbMsSQLIndexBean.java @@ -0,0 +1,47 @@ +package com.alfresco.support.alfrescodb.beans; + +import java.io.Serializable; +import java.math.BigInteger; +import java.sql.Timestamp; + +import com.fasterxml.jackson.annotation.JsonFormat; + +public class DbMsSQLIndexBean implements Serializable { + private String tableName; + private String indexName; + private Integer indexID; + private BigInteger indexSizeBytes; + @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ssXXX") + private Timestamp statisticsUpdateDate; + + public String getTableName() { + return tableName; + } + public void setTableName(String tableName) { + this.tableName = tableName; + } + public String getIndexName() { + return indexName; + } + public void setIndexName(String indexName) { + this.indexName = indexName; + } + public Integer getIndexID() { + return indexID; + } + public void setIndexID(Integer indexID) { + this.indexID = indexID; + } + public BigInteger getIndexSizeBytes() { + return indexSizeBytes; + } + public void setIndexSizeBytes(BigInteger indexSizeBytes) { + this.indexSizeBytes = indexSizeBytes; + } + public Timestamp getStatisticsUpdateDate() { + return statisticsUpdateDate; + } + public void setStatisticsUpdateDate(Timestamp statisticsUpdateDate) { + this.statisticsUpdateDate = statisticsUpdateDate; + } +} diff --git a/src/main/java/com/alfresco/support/alfrescodb/beans/DbMsSQLTableBean.java b/src/main/java/com/alfresco/support/alfrescodb/beans/DbMsSQLTableBean.java new file mode 100644 index 0000000..3945cb7 --- /dev/null +++ b/src/main/java/com/alfresco/support/alfrescodb/beans/DbMsSQLTableBean.java @@ -0,0 +1,47 @@ +package com.alfresco.support.alfrescodb.beans; + +import java.io.Serializable; +import java.math.BigInteger; +import java.sql.Timestamp; + +import com.fasterxml.jackson.annotation.JsonFormat; + +public class DbMsSQLTableBean implements Serializable { + private String tableName; + private BigInteger totalReservedBytes; + private BigInteger usedSpaceBytes; + private BigInteger rowsCount; + @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ssXXX") + private Timestamp statisticsUpdateDate; + + public String getTableName() { + return tableName; + } + public void setTableName(String tableName) { + this.tableName = tableName; + } + public BigInteger getTotalReservedBytes() { + return totalReservedBytes; + } + public void setTotalReservedBytes(BigInteger totalReservedBytes) { + this.totalReservedBytes = totalReservedBytes; + } + public BigInteger getUsedSpaceBytes() { + return usedSpaceBytes; + } + public void setUsedSpaceBytes(BigInteger usedSpaceBytes) { + this.usedSpaceBytes = usedSpaceBytes; + } + public BigInteger getRowsCount() { + return rowsCount; + } + public void setRowsCount(BigInteger rowsCount) { + this.rowsCount = rowsCount; + } + public Timestamp getStatisticsUpdateDate() { + return statisticsUpdateDate; + } + public void setStatisticsUpdateDate(Timestamp statisticsUpdateDate) { + this.statisticsUpdateDate = statisticsUpdateDate; + } +} diff --git a/src/main/java/com/alfresco/support/alfrescodb/controllers/WebController.java b/src/main/java/com/alfresco/support/alfrescodb/controllers/WebController.java index 3c933cb..eda03f4 100644 --- a/src/main/java/com/alfresco/support/alfrescodb/controllers/WebController.java +++ b/src/main/java/com/alfresco/support/alfrescodb/controllers/WebController.java @@ -15,6 +15,8 @@ import com.alfresco.support.alfrescodb.beans.AppliedPatchesBean; import com.alfresco.support.alfrescodb.beans.ArchivedNodesBean; import com.alfresco.support.alfrescodb.beans.ContentModelBean; +import com.alfresco.support.alfrescodb.beans.DbMsSQLIndexBean; +import com.alfresco.support.alfrescodb.beans.DbMsSQLTableBean; import com.alfresco.support.alfrescodb.beans.DbMySQLBean; import com.alfresco.support.alfrescodb.beans.DbPostgresBean; import com.alfresco.support.alfrescodb.beans.JmxPropertiesBean; @@ -84,8 +86,11 @@ public String dbSize(Model model) { model.addAttribute("listRelationInfosMySQL", listDbMySQL); //} else if ("oracle".equalsIgnoreCase(appProperties.getDbType())) { // XXX TODO - //} else if ("microsoft".equalsIgnoreCase(appProperties.getDbType())) { - // XXX TODO + } else if ("microsoft".equalsIgnoreCase(appProperties.getDbType())) { + List listMsSQLTable = exportMapper.findTablesInfoMSSql(); + model.addAttribute("listMsSQLTable", listMsSQLTable); + List listMsSQLIndex = exportMapper.findIndexesInfoMSSql(); + model.addAttribute("listMsSQLIndex", listMsSQLIndex); } else { throw new IllegalArgumentException("DB Type not recognized: " + appProperties.getDbType()); } @@ -232,7 +237,15 @@ public void authorities(Model model) { if (appProperties.getIsEnterpriseVersion()) { // Count authorized users - String countAuthorizedUsers = exportMapper.countAuthorizedUsers(); + String countAuthorizedUsers = ""; + if ("oracle".equalsIgnoreCase(appProperties.getDbType())) { + //NOOP + } else if ("microsoft".equalsIgnoreCase(appProperties.getDbType())) { + countAuthorizedUsers = exportMapper.countAuthorizedUsersMicrosoft(); + } else { + countAuthorizedUsers = exportMapper.countAuthorizedUsers(); + } + model.addAttribute("countAuthorizedUsers", countAuthorizedUsers); model.addAttribute("isEnterpriseVersion", appProperties.getIsEnterpriseVersion()); } diff --git a/src/main/java/com/alfresco/support/alfrescodb/export/ExportComponent.java b/src/main/java/com/alfresco/support/alfrescodb/export/ExportComponent.java index 5e8bd35..331edce 100644 --- a/src/main/java/com/alfresco/support/alfrescodb/export/ExportComponent.java +++ b/src/main/java/com/alfresco/support/alfrescodb/export/ExportComponent.java @@ -18,6 +18,8 @@ import com.alfresco.support.alfrescodb.beans.AppliedPatchesBean; import com.alfresco.support.alfrescodb.beans.ArchivedNodesBean; import com.alfresco.support.alfrescodb.beans.ContentModelBean; +import com.alfresco.support.alfrescodb.beans.DbMsSQLIndexBean; +import com.alfresco.support.alfrescodb.beans.DbMsSQLTableBean; import com.alfresco.support.alfrescodb.beans.DbMySQLBean; import com.alfresco.support.alfrescodb.beans.DbPostgresBean; import com.alfresco.support.alfrescodb.beans.LargeFolderBean; @@ -62,7 +64,10 @@ public void exportReport(Model model) { } else if ("oracle".equalsIgnoreCase(appProperties.getDbType())) { // XXX TODO } else if ("microsoft".equalsIgnoreCase(appProperties.getDbType())) { - // XXX TODO + List listDbMsSQLTables = exportMapper.findTablesInfoMSSql(); + generatedFiles.add(this.exportToFile(listDbMsSQLTables, "listDbMsSQLTables")); + List listDbMsSQLIndexes = exportMapper.findIndexesInfoMSSql(); + generatedFiles.add(this.exportToFile(listDbMsSQLIndexes, "listDbMsSQLIndexes")); } /* Large Folders */ @@ -132,7 +137,13 @@ public void exportReport(Model model) { generatedFiles.add(this.exportToFile(countTotalUsers, "countTotalUsers")); if (appProperties.getIsEnterpriseVersion()) { - String countAuthorizedUsers = exportMapper.countAuthorizedUsers(); + String countAuthorizedUsers = ""; + if ("oracle".equalsIgnoreCase(appProperties.getDbType())) { + } else if ("microsoft".equalsIgnoreCase(appProperties.getDbType())) { + countAuthorizedUsers = exportMapper.countAuthorizedUsersMicrosoft(); + } else { + countAuthorizedUsers = exportMapper.countAuthorizedUsers(); + } generatedFiles.add(this.exportToFile(countAuthorizedUsers, "countAuthorizedUsers")); } diff --git a/src/main/resources/templates/dbSize.html b/src/main/resources/templates/dbSize.html index 3fa3452..a5fb69e 100644 --- a/src/main/resources/templates/dbSize.html +++ b/src/main/resources/templates/dbSize.html @@ -218,39 +218,39 @@ -
+
- Tables Size + Tables
+ + - - - + - - - - - - + + + + + +
Table NameTotal Space (Bytes)Used Space (Bytes) Rows CountTotal Space MBUsed Space MBUnused Space MBLast updated
-
+
- Indexes Size + Indexes
@@ -258,14 +258,18 @@ - + + + - - - - + + + + + +
Table Name Index NameIndex Size MBIndex IDSize (Bytes)Last Updated