最后我们来看一下事务共享区域。 首先仍然是每个进程都有的一个数据结构__db_txnmgr{}: /** The transaction manager encapsulates the transaction system. It contains* references to the log and lock managers as well as the state that keeps* track
最后我们来看一下事务共享区域。
首先仍然是每个进程都有的一个数据结构__db_txnmgr{}:
<code>/* * The transaction manager encapsulates the transaction system. It contains * references to the log and lock managers as well as the state that keeps * track of the shared memory region. */ struct __db_txnmgr { /* These fields need to be protected for multi-threaded support. */ db_mutex_t *mutexp; /* Synchronization. */ /* list of active transactions */ TAILQ_HEAD(_chain, __db_txn) txn_chain; /* These fields are not protected. */ REGINFO reginfo; /* Region information. */ DB_ENV *dbenv; /* Environment. */ int (*recover) /* Recovery dispatch routine */ __P((DB_LOG *, DBT *, DB_LSN *, int, void *)); u_int32_t flags; /* DB_TXN_NOSYNC, DB_THREAD */ DB_TXNREGION *region; /* address of shared memory region */ void *mem; /* address of the shalloc space */ }; </code>
同样拥有一个指向描述共享区域的reginfo字段和指向共享区域的指针mem。
<code>/* * Layout of the shared memory region. * The region consists of a DB_TXNREGION structure followed by a large * pool of shalloc'd memory which is used to hold TXN_DETAIL structures * and thread mutexes (which are dynamically allocated). */ struct __db_txnregion { RLAYOUT hdr; /* Shared memory region header. */ u_int32_t magic; /* transaction magic number */ u_int32_t version; /* version number */ u_int32_t maxtxns; /* maximum number of active txns */ u_int32_t last_txnid; /* last transaction id given out */ DB_LSN pending_ckp; /* last checkpoint did not finish */ DB_LSN last_ckp; /* lsn of the last checkpoint */ time_t time_ckp; /* time of last checkpoint */ u_int32_t logtype; /* type of logging */ u_int32_t locktype; /* lock type */ u_int32_t naborts; /* number of aborted transactions */ u_int32_t ncommits; /* number of committed transactions */ u_int32_t nbegins; /* number of begun transactions */ SH_TAILQ_HEAD(_active) active_txn; /* active transaction list */ }; </code>
共享区域除了有个公用的头部hdr字段以外,最重要的是有一个所有活动事务的列表active_txn字段。
现在让我们看一下这些数据结构和共享区域的创建或打开以及初始化操作。
在db_appinit()函数中调用txn_open()函数打开或连接一个事务共享区域:
<code>if (LF_ISSET(DB_INIT_TXN) && (ret = txn_open(NULL, LF_ISSET(DB_CREATE | DB_THREAD | DB_TXN_NOSYNC), mode, dbenv, &dbenv->tx_info)) != 0) goto err; </code>
其中txn_open定义如下:
<code>int txn_open(path, flags, mode, dbenv, mgrpp) const char *path; u_int32_t flags; int mode; DB_ENV *dbenv; DB_TXNMGR **mgrpp; { DB_TXNMGR *tmgrp; u_int32_t maxtxns; int ret; /* Validate arguments. */ 首先仍然是验证参数 然后用malloc创建每个进程都有的事务管理器结构 /* Now, create the transaction manager structure and set its fields. */ if ((ret = __os_calloc(1, sizeof(DB_TXNMGR), &tmgrp)) != 0) return (ret); 并初始化事务管理器结构 /* Initialize the transaction manager structure. */ tmgrp->mutexp = NULL; tmgrp->dbenv = dbenv; tmgrp->recover = dbenv->tx_recover == NULL ? __db_dispatch : dbenv->tx_recover; tmgrp->flags = LF_ISSET(DB_TXN_NOSYNC | DB_THREAD); TAILQ_INIT(&tmgrp->txn_chain); 现在开始创建或连接到一个事务共享区域 /* Join/create the txn region. */ 首先还是一样,填充区域描述信息REGINFO{},然后调用__db_rattach()函数创建或打开一个共享区域 tmgrp->reginfo.dbenv = dbenv; tmgrp->reginfo.appname = DB_APP_NONE; if (path == NULL) tmgrp->reginfo.path = NULL; else if ((ret = __os_strdup(path, &tmgrp->reginfo.path)) != 0) goto err; tmgrp->reginfo.file = DEFAULT_TXN_FILE; tmgrp->reginfo.mode = mode; tmgrp->reginfo.size = TXN_REGION_SIZE(maxtxns); tmgrp->reginfo.dbflags = flags; tmgrp->reginfo.addr = NULL; tmgrp->reginfo.fd = -1; tmgrp->reginfo.flags = dbenv->tx_max == 0 ? REGION_SIZEDEF : 0; if ((ret = __db_rattach(&tmgrp->reginfo)) != 0) goto err; /* Fill in region-related fields. */ tmgrp->region = tmgrp->reginfo.addr; tmgrp->mem = &tmgrp->region[1]; if (F_ISSET(&tmgrp->reginfo, REGION_CREATED)) { tmgrp->region->maxtxns = maxtxns; if ((ret = __txn_init(tmgrp->region)) != 0) goto err; } else if (tmgrp->region->magic != DB_TXNMAGIC) { /* Check if valid region. */ __db_err(dbenv, "txn_open: Bad magic number"); ret = EINVAL; goto err; } // 如果这是一个新的事务共享区域,初始化之。这里又和MPOOL一样, // 使用__shmalloc函数管理共享区域内存 if (LF_ISSET(DB_THREAD)) { if ((ret = __db_shalloc(tmgrp->mem, sizeof(db_mutex_t), MUTEX_ALIGNMENT, &tmgrp->mutexp)) == 0) /* * Since we only get here if threading is turned on, we * know that we have spinlocks, so the offset is going * to be ignored. We put 0 here as a valid placeholder. */ __db_mutex_init(tmgrp->mutexp, 0); if (ret != 0) goto err; } UNLOCK_TXNREGION(tmgrp); *mgrpp = tmgrp; return (0); err: if (tmgrp->reginfo.addr != NULL) { if (tmgrp->mutexp != NULL) __db_shalloc_free(tmgrp->mem, tmgrp->mutexp); UNLOCK_TXNREGION(tmgrp); (void)__db_rdetach(&tmgrp->reginfo); if (F_ISSET(&tmgrp->reginfo, REGION_CREATED)) (void)txn_unlink(path, 1, dbenv); } if (tmgrp->reginfo.path != NULL) __os_freestr(tmgrp->reginfo.path); __os_free(tmgrp, sizeof(*tmgrp)); return (ret); } </code>
这个函数就是txn_open()调用来初始化事务共享区域的函数:
<code>/* * This file contains the top level routines of the transaction library. * It assumes that a lock manager and log manager that conform to the db_log(3) * and db_lock(3) interfaces exist. * * Initialize a transaction region in shared memory. * Return 0 on success, errno on failure. */ static int __txn_init(txn_region) DB_TXNREGION *txn_region; { time_t now; (void)time(&now); /* maxtxns is already initialized. */ txn_region->magic = DB_TXNMAGIC; txn_region->version = DB_TXNVERSION; txn_region->last_txnid = TXN_MINIMUM; /* * XXX * If we ever do more types of locking and logging, this changes. */ txn_region->logtype = 0; txn_region->locktype = 0; txn_region->time_ckp = now; ZERO_LSN(txn_region->last_ckp); ZERO_LSN(txn_region->pending_ckp); SH_TAILQ_INIT(&txn_region->active_txn); __db_shalloc_init((void *)&txn_region[1], TXN_REGION_SIZE(txn_region->maxtxns) - sizeof(DB_TXNREGION)); return (0); } </code>
其中DB_TXN{}是每个事务的描述符(定义在db_int.h文件中):
<code>/* The structure allocated for every transaction. */ struct __db_txn { DB_TXNMGR *mgrp; /* Pointer to transaction manager. */ DB_TXN *parent; /* Pointer to transaction's parent. */ DB_LSN last_lsn; /* Lsn of last log write. */ u_int32_t txnid; /* Unique transaction id. */ size_t off; /* Detail structure within region. */ TAILQ_ENTRY(__db_txn) links; /* Links transactions off manager. */ TAILQ_HEAD(__kids, __db_txn) kids; /* Child transactions. */ TAILQ_ENTRY(__db_txn) klinks; /* Links child transactions. */ #define TXN_MALLOC 0x01 /* Structure allocated by TXN system. */ u_int32_t flags; }; typedef struct __txn_detail { u_int32_t txnid; /* current transaction id used to link free list also */ DB_LSN last_lsn; /* last lsn written for this txn */ DB_LSN begin_lsn; /* lsn of begin record */ size_t last_lock; /* offset in lock region of last lock for this transaction. */ size_t parent; /* Offset of transaction's parent. */ #define TXN_UNALLOC 0 #define TXN_RUNNING 1 #define TXN_ABORTED 2 #define TXN_PREPARED 3 #define TXN_COMMITTED 4 u_int32_t status; /* status of the transaction */ SH_TAILQ_ENTRY links; /* free/active list */ #define TXN_XA_ABORTED 1 #define TXN_XA_DEADLOCKED 2 #define TXN_XA_ENDED 3 #define TXN_XA_PREPARED 4 #define TXN_XA_STARTED 5 #define TXN_XA_SUSPENDED 6 u_int32_t xa_status; /* XA status */ /* * XID (xid_t) structure: because these fields are logged, the * sizes have to be explicit. */ DB_XID xid; /* XA global transaction id */ u_int32_t bqual; /* bqual_length from XID */ u_int32_t gtrid; /* gtrid_length from XID */ int32_t format; /* XA format */ } TXN_DETAIL; </code>
原文地址:BDB事务共享区域, 感谢原作者分享。

MySQL은 오픈 소스 관계형 데이터베이스 관리 시스템으로, 주로 데이터를 신속하고 안정적으로 저장하고 검색하는 데 사용됩니다. 작업 원칙에는 클라이언트 요청, 쿼리 해상도, 쿼리 실행 및 반환 결과가 포함됩니다. 사용의 예로는 테이블 작성, 데이터 삽입 및 쿼리 및 조인 작업과 같은 고급 기능이 포함됩니다. 일반적인 오류에는 SQL 구문, 데이터 유형 및 권한이 포함되며 최적화 제안에는 인덱스 사용, 최적화 된 쿼리 및 테이블 분할이 포함됩니다.

MySQL은 데이터 저장, 관리, 쿼리 및 보안에 적합한 오픈 소스 관계형 데이터베이스 관리 시스템입니다. 1. 다양한 운영 체제를 지원하며 웹 응용 프로그램 및 기타 필드에서 널리 사용됩니다. 2. 클라이언트-서버 아키텍처 및 다양한 스토리지 엔진을 통해 MySQL은 데이터를 효율적으로 처리합니다. 3. 기본 사용에는 데이터베이스 및 테이블 작성, 데이터 삽입, 쿼리 및 업데이트가 포함됩니다. 4. 고급 사용에는 복잡한 쿼리 및 저장 프로 시저가 포함됩니다. 5. 설명 진술을 통해 일반적인 오류를 디버깅 할 수 있습니다. 6. 성능 최적화에는 인덱스의 합리적인 사용 및 최적화 된 쿼리 문이 포함됩니다.

MySQL은 성능, 신뢰성, 사용 편의성 및 커뮤니티 지원을 위해 선택됩니다. 1.MYSQL은 효율적인 데이터 저장 및 검색 기능을 제공하여 여러 데이터 유형 및 고급 쿼리 작업을 지원합니다. 2. 고객-서버 아키텍처 및 다중 스토리지 엔진을 채택하여 트랜잭션 및 쿼리 최적화를 지원합니다. 3. 사용하기 쉽고 다양한 운영 체제 및 프로그래밍 언어를 지원합니다. 4. 강력한 지역 사회 지원을 받고 풍부한 자원과 솔루션을 제공합니다.

InnoDB의 잠금 장치에는 공유 잠금 장치, 독점 잠금, 의도 잠금 장치, 레코드 잠금, 갭 잠금 및 다음 키 잠금 장치가 포함됩니다. 1. 공유 잠금을 사용하면 다른 트랜잭션을 읽지 않고 트랜잭션이 데이터를 읽을 수 있습니다. 2. 독점 잠금은 다른 트랜잭션이 데이터를 읽고 수정하는 것을 방지합니다. 3. 의도 잠금은 잠금 효율을 최적화합니다. 4. 레코드 잠금 잠금 인덱스 레코드. 5. 갭 잠금 잠금 장치 색인 기록 간격. 6. 다음 키 잠금은 데이터 일관성을 보장하기 위해 레코드 잠금과 갭 잠금의 조합입니다.

MySQL 쿼리 성능이 좋지 않은 주된 이유는 인덱스 사용, 쿼리 최적화에 의한 잘못된 실행 계획 선택, 불합리한 테이블 디자인, 과도한 데이터 볼륨 및 잠금 경쟁이 포함됩니다. 1. 색인이 느리게 쿼리를 일으키지 않으며 인덱스를 추가하면 성능이 크게 향상 될 수 있습니다. 2. 설명 명령을 사용하여 쿼리 계획을 분석하고 Optimizer 오류를 찾으십시오. 3. 테이블 구조를 재구성하고 결합 조건을 최적화하면 테이블 설계 문제가 향상 될 수 있습니다. 4. 데이터 볼륨이 크면 분할 및 테이블 디비전 전략이 채택됩니다. 5. 높은 동시성 환경에서 거래 및 잠금 전략을 최적화하면 잠금 경쟁이 줄어들 수 있습니다.

데이터베이스 최적화에서 쿼리 요구 사항에 따라 인덱싱 전략을 선택해야합니다. 1. 쿼리에 여러 열이 포함되고 조건 순서가 수정되면 복합 인덱스를 사용하십시오. 2. 쿼리에 여러 열이 포함되어 있지만 조건 순서가 고정되지 않은 경우 여러 단일 열 인덱스를 사용하십시오. 복합 인덱스는 다중 열 쿼리를 최적화하는 데 적합한 반면 단일 열 인덱스는 단일 열 쿼리에 적합합니다.

MySQL 느린 쿼리를 최적화하려면 SlowQueryLog 및 Performance_Schema를 사용해야합니다. 1. SlowQueryLog 및 Set Stresholds를 사용하여 느린 쿼리를 기록합니다. 2. Performance_schema를 사용하여 쿼리 실행 세부 정보를 분석하고 성능 병목 현상을 찾고 최적화하십시오.

MySQL 및 SQL은 개발자에게 필수적인 기술입니다. 1.MySQL은 오픈 소스 관계형 데이터베이스 관리 시스템이며 SQL은 데이터베이스를 관리하고 작동하는 데 사용되는 표준 언어입니다. 2.MYSQL은 효율적인 데이터 저장 및 검색 기능을 통해 여러 스토리지 엔진을 지원하며 SQL은 간단한 문을 통해 복잡한 데이터 작업을 완료합니다. 3. 사용의 예에는 기본 쿼리 및 조건 별 필터링 및 정렬과 같은 고급 쿼리가 포함됩니다. 4. 일반적인 오류에는 구문 오류 및 성능 문제가 포함되며 SQL 문을 확인하고 설명 명령을 사용하여 최적화 할 수 있습니다. 5. 성능 최적화 기술에는 인덱스 사용, 전체 테이블 스캔 피하기, 조인 작업 최적화 및 코드 가독성 향상이 포함됩니다.


핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

AI Hentai Generator
AI Hentai를 무료로 생성하십시오.

인기 기사

뜨거운 도구

MinGW - Windows용 미니멀리스트 GNU
이 프로젝트는 osdn.net/projects/mingw로 마이그레이션되는 중입니다. 계속해서 그곳에서 우리를 팔로우할 수 있습니다. MinGW: GCC(GNU Compiler Collection)의 기본 Windows 포트로, 기본 Windows 애플리케이션을 구축하기 위한 무료 배포 가능 가져오기 라이브러리 및 헤더 파일로 C99 기능을 지원하는 MSVC 런타임에 대한 확장이 포함되어 있습니다. 모든 MinGW 소프트웨어는 64비트 Windows 플랫폼에서 실행될 수 있습니다.

Eclipse용 SAP NetWeaver 서버 어댑터
Eclipse를 SAP NetWeaver 애플리케이션 서버와 통합합니다.

메모장++7.3.1
사용하기 쉬운 무료 코드 편집기

Dreamweaver Mac版
시각적 웹 개발 도구

SublimeText3 Linux 새 버전
SublimeText3 Linux 최신 버전
