jjzjj

Android sqlite begintransaction 执行时间过长

coder 2023-12-22 原文

我想把json解析出来的数据批量插入到db中。我使用下面的方法插入批处理。问题是 mDbWritable.beginTransaction();执行时间太长。通常像6秒!我不知道问题在哪里。一些想法是什么导致执行时间这么长?非常感谢。

@Override
public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations)
        throws OperationApplicationException {
    long start = System.currentTimeMillis();
    mDbWritable.beginTransaction();
    long time = System.currentTimeMillis() - start;
    Alog.i(TAG, "Time applyBatch beginTransaction: " + time);

    final int numOperations = operations.size();
    final ContentProviderResult[] results = new ContentProviderResult[numOperations];
    try {
        for (int i = 0; i < numOperations; i++) {
            results[i] = operations.get(i).apply(this, results, i);
        }

        mDbWritable.setTransactionSuccessful();

    } finally {
        mDbWritable.endTransaction();
    }
    return results;
}

一些来自日志的例子:

11-16 15:14:53.726: I/ApiProvider(21442): Time applyBatch beginTransaction: 6025
11-16 15:15:00.713: I/ApiProvider(21442): Time applyBatch beginTransaction: 4940
11-16 15:15:17.819: I/ApiProvider(21442): Time applyBatch beginTransaction: 8651
11-16 15:15:45.346: I/ApiProvider(21442): Time applyBatch beginTransaction: 12672
11-16 15:16:16.807: I/ApiProvider(21442): Time applyBatch beginTransaction: 12411
11-16 15:16:45.685: I/ApiProvider(21442): Time applyBatch beginTransaction: 12247
11-16 15:17:01.500: I/ApiProvider(21442): Time applyBatch beginTransaction: 12788

编辑:我在解析 json 时在循环中使用 apply batch。例如对于 json 中的每个项目 - 解析并应用批处理。批处理包含插入、更新、删除操作。

这是我如何迭代和调用 applyBatch 的代码

Cursor starredChannelsCursor =
        mContentResolver.query(ApiContract.Channels.CONTENT_URI,
                               new String[] {BaseColumns._ID, ChannelsTable.ID, ChannelsTable.SLUG },
                               ChannelsTable.IS_STARRED + "=?",new String[] { "1" }, null);

String userName = mSettings.getUserName();

if (starredChannelsCursor != null && starredChannelsCursor.moveToFirst()) {     
    while (!starredChannelsCursor.isAfterLast()) {
        String channelSlug = starredChannelsCursor.getString(2);
        ChannelHandler channelHandler = new ChannelHandler(this);
        URI channelApiUri = Constants.getChannelApiURI(channelSlug,userName);
        //execute update make applybatch call
        executeUpdate(channelApiUri, channelHandler);

        starredChannelsCursor.moveToNext();
    }
}

if (starredChannelsCursor != null) {
    starredChannelsCursor.close();
}


/**
* Make call to Uri, parse response and apply batch operations to
* contentResolver
* 
* @param apiUri
* @param handler
*            - handles parsing
*/
private boolean executeUpdate(URI apiUri, AbstractJSONHandler handler) {
    ApiResponse apiResponse = mHttpHelper.doHttpCall(apiUri);

    ArrayList<ContentProviderOperation> batch =
                    new ArrayList<ContentProviderOperation>();

    if (apiResponse != null) {
        batch = handler.parse(apiResponse);
        Alog.v(TAG, "update user data from " + apiUri);
    }

    if (batch.size() > 0) {
        try {
            mContentResolver.applyBatch(ApiContract.CONTENT_AUTHORITY, batch);
        } catch (Exception e) {
            Alog.v(TAG, "Error: " + e.getMessage());
        }
    }
    return true;
}

最佳答案

似乎唯一可能出现的问题是,不同的线程在调用 beginTransaction() 时获取相同的锁,并浪费时间等待其他线程释放锁。查看您的代码,了解您如何管理线程以及从哪些线程调用 applyBatch(..) 方法。

查看位于 SQLiteDatabasebeginTransaction() 的调用层次结构可能对您也很有用。类。

关于Android sqlite begintransaction 执行时间过长,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13418569/

有关Android sqlite begintransaction 执行时间过长的更多相关文章

  1. ruby-openid:执行发现时未设置@socket - 2

    我在使用omniauth/openid时遇到了一些麻烦。在尝试进行身份验证时,我在日志中发现了这一点:OpenID::FetchingError:Errorfetchinghttps://www.google.com/accounts/o8/.well-known/host-meta?hd=profiles.google.com%2Fmy_username:undefinedmethod`io'fornil:NilClass重要的是undefinedmethodio'fornil:NilClass来自openid/fetchers.rb,在下面的代码片段中:moduleNetclass

  2. ruby - Chef 执行非顺序配方 - 2

    我遵循了教程http://gettingstartedwithchef.com/,第1章。我的运行list是"run_list":["recipe[apt]","recipe[phpap]"]我的phpapRecipe默认Recipeinclude_recipe"apache2"include_recipe"build-essential"include_recipe"openssl"include_recipe"mysql::client"include_recipe"mysql::server"include_recipe"php"include_recipe"php::modul

  3. ruby-on-rails - Ruby 检查日期时间是否为 iso8601 并保存 - 2

    我需要检查DateTime是否采用有效的ISO8601格式。喜欢:#iso8601?我检查了ruby​​是否有特定方法,但没有找到。目前我正在使用date.iso8601==date来检查这个。有什么好的方法吗?编辑解释我的环境,并改变问题的范围。因此,我的项目将使用jsapiFullCalendar,这就是我需要iso8601字符串格式的原因。我想知道更好或正确的方法是什么,以正确的格式将日期保存在数据库中,或者让ActiveRecord完成它们的工作并在我需要时间信息时对其进行操作。 最佳答案 我不太明白你的问题。我假设您想检查

  4. ruby - 为什么 Ruby 的 each 迭代器先执行? - 2

    我在用Ruby执行简单任务时遇到了一件奇怪的事情。我只想用每个方法迭代字母表,但迭代在执行中先进行:alfawit=("a".."z")puts"That'sanalphabet:\n\n#{alfawit.each{|litera|putslitera}}"这段代码的结果是:(缩写)abc⋮xyzThat'sanalphabet:a..z知道为什么它会这样工作或者我做错了什么吗?提前致谢。 最佳答案 因为您的each调用被插入到在固定字符串之前执行的字符串文字中。此外,each返回一个Enumerable,实际上您甚至打印它。试试

  5. ruby-on-rails - 将 Ruby 中的日期/时间格式化为 YYYY-MM-DD HH :MM:SS - 2

    这个问题在这里已经有了答案:Railsformattingdate(4个答案)关闭4年前。我想格式化Time.Now函数以显示YYYY-MM-DDHH:MM:SS而不是:“2018-03-0909:47:19+0000”该函数需要放在时间中.现在功能。require‘roo’require‘roo-xls’require‘byebug’file_name=ARGV.first||“Template.xlsx”excel_file=Roo::Spreadsheet.open(“./#{file_name}“,extension::xlsx)xml=Nokogiri::XML::Build

  6. ruby - 查找字符串中的内容类型(数字、日期、时间、字符串等) - 2

    我正在尝试解析一个CSV文件并使用SQL命令自动为其创建一个表。CSV中的第一行给出了列标题。但我需要推断每个列的类型。Ruby中是否有任何函数可以找到每个字段中内容的类型。例如,CSV行:"12012","Test","1233.22","12:21:22","10/10/2009"应该产生像这样的类型['integer','string','float','time','date']谢谢! 最佳答案 require'time'defto_something(str)if(num=Integer(str)rescueFloat(s

  7. ruby - 检查是否通过 require 执行或导入了 Ruby 程序 - 2

    如何检查Ruby文件是否是通过“require”或“load”导入的,而不是简单地从命令行执行的?例如:foo.rb的内容:puts"Hello"bar.rb的内容require'foo'输出:$./foo.rbHello$./bar.rbHello基本上,我想调用bar.rb以不执行puts调用。 最佳答案 将foo.rb改为:if__FILE__==$0puts"Hello"end检查__FILE__-当前ruby​​文件的名称-与$0-正在运行的脚本的名称。 关于ruby-检查是否

  8. postman——集合——执行集合——测试脚本——pm对象简单示例02 - 2

    //1.验证返回状态码是否是200pm.test("Statuscodeis200",function(){pm.response.to.have.status(200);});//2.验证返回body内是否含有某个值pm.test("Bodymatchesstring",function(){pm.expect(pm.response.text()).to.include("string_you_want_to_search");});//3.验证某个返回值是否是100pm.test("Yourtestname",function(){varjsonData=pm.response.json

  9. ruby-on-rails - rbenv:从 RVM 移动到 rbenv 后,在 Jenkins 执行 shell 中找不到命令 - 2

    我从Ubuntu服务器上的RVM转移到rbenv。当我使用RVM时,使用bundle没有问题。转移到rbenv后,我在Jenkins的执行shell中收到“找不到命令”错误。我内爆并删除了RVM,并从~/.bashrc'中删除了所有与RVM相关的行。使用后我仍然收到此错误:rvmimploderm~/.rvm-rfrm~/.rvmrcgeminstallbundlerecho'exportPATH="$HOME/.rbenv/bin:$PATH"'>>~/.bashrcecho'eval"$(rbenvinit-)"'>>~/.bashrc.~/.bashrcrbenvversions

  10. sql - 查询忽略时间戳日期的时间范围 - 2

    我正在尝试查询我的Rails数据库(Postgres)中的购买表,我想查询时间范围。例如,我想知道在所有日期的下午2点到3点之间进行了多少次购买。此表中有一个created_at列,但我不知道如何在不搜索特定日期的情况下完成此操作。我试过:Purchases.where("created_atBETWEEN?and?",Time.now-1.hour,Time.now)但这最终只会搜索今天与那些时间的日期。 最佳答案 您需要使用PostgreSQL'sdate_part/extractfunction从created_at中提取小时

随机推荐