@@ -306,6 +306,10 @@ synchronized void setLastSqlStatement(String sql) {
306306 /** Used to find out where this object was created in case it never got closed. */
307307 private final Throwable mStackTrace ;
308308
309+ // System property that enables logging of slow queries. Specify the threshold in ms.
310+ private static final String LOG_SLOW_QUERIES_PROPERTY = "db.log.slow_query_threshold" ;
311+ private final int mSlowQueryThreshold ;
312+
309313 /** stores the list of statement ids that need to be finalized by sqlite */
310314 private final ArrayList <Integer > mClosedStatementIds = new ArrayList <Integer >();
311315
@@ -1555,6 +1559,11 @@ public Cursor rawQueryWithFactory(
15551559 String editTable ) {
15561560 verifyDbIsOpen ();
15571561 BlockGuard .getThreadPolicy ().onReadFromDisk ();
1562+ long timeStart = 0 ;
1563+
1564+ if (false || mSlowQueryThreshold != -1 ) {
1565+ timeStart = System .currentTimeMillis ();
1566+ }
15581567
15591568 SQLiteDatabase db = getDbConnection (sql );
15601569 SQLiteCursorDriver driver = new SQLiteDirectCursorDriver (db , sql , editTable );
@@ -1565,6 +1574,24 @@ public Cursor rawQueryWithFactory(
15651574 cursorFactory != null ? cursorFactory : mFactory ,
15661575 selectionArgs );
15671576 } finally {
1577+ if (false || mSlowQueryThreshold != -1 ) {
1578+
1579+ // Force query execution
1580+ int count = -1 ;
1581+ if (cursor != null ) {
1582+ count = cursor .getCount ();
1583+ }
1584+
1585+ long duration = System .currentTimeMillis () - timeStart ;
1586+
1587+ if (false || duration >= mSlowQueryThreshold ) {
1588+ Log .v (SQLiteCursor .TAG ,
1589+ "query (" + duration + " ms): " + driver .toString () + ", args are "
1590+ + (selectionArgs != null
1591+ ? TextUtils .join ("," , selectionArgs )
1592+ : "<null>" ) + ", count is " + count );
1593+ }
1594+ }
15681595 releaseDbConnection (db );
15691596 }
15701597 return cursor ;
@@ -1940,6 +1967,7 @@ private SQLiteDatabase(String path, CursorFactory factory, int flags,
19401967 setMaxSqlCacheSize (DEFAULT_SQL_CACHE_SIZE );
19411968 mFlags = flags ;
19421969 mPath = path ;
1970+ mSlowQueryThreshold = SystemProperties .getInt (LOG_SLOW_QUERIES_PROPERTY , -1 );
19431971 mStackTrace = new DatabaseObjectNotClosedException ().fillInStackTrace ();
19441972 mFactory = factory ;
19451973 mPrograms = new WeakHashMap <SQLiteClosable ,Object >();
0 commit comments